【问题标题】:Convert country name to int in panda在熊猫中将国家名称转换为int
【发布时间】:2020-08-07 12:50:12
【问题描述】:
pd.Series(Confirmed_df["Country/Region"]).convert_objects(convert_numeric= True)

FutureWarning:convert_objects 已弃用。要重新推断对象列的数据 dtypes,请使用 Series.infer_objects() 对于所有其他转换,请使用特定于数据类型的转换器 pd.to_datetime、pd.to_timedelta 和 pd.to_numeric。 从 sys.path 中删除 cwd 后。

输出:

0                 Afghanistan
1                     Albania
2                     Algeria
3                     Andorra
4                      Angola
5         Antigua and Barbuda
6                   Argentina
7                     Armenia
8                   Australia
9                   Australia
10                  Australia
11                  Australia
12                  Australia
13                  Australia
14                  Australia
15                  Australia
16                    Austria
17                 Azerbaijan
18                    Bahamas
19                    Bahrain
20                 Bangladesh
21                   Barbados
22                    Belarus
23                    Belgium
24                      Benin
25                     Bhutan
26                    Bolivia
27     Bosnia and Herzegovin

但它仍然是(对象)我想转换为 int 类型。

【问题讨论】:

  • 你想要的输出是什么?
  • 我想将类型从object转换为int
  • 您无法将字符串转换为 int。

标签: python pandas object int


【解决方案1】:

重症监护病房:

df=pd.DataFrame({'Country':['Antigua and Barbuda','Argentina','Kenya']})
codes,uniques=pd.factorize(df.Country)
codes

显然,要转换为列

df['x']=pd.Series(codes)
df

【讨论】:

  • 我以为你想对它们进行编码。无论如何我都会留下我的答案。也许提供有关预期输出的更多信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多