所以我有一个数据集,我的输出是:
gdp = pd.read_csv(r"gdpproject.csv",
encoding="ISO-8859-1")
gdp.head(2)
gdp.tail(2)
这将为我提供以下输出:
Country.Name Indicator.Name 2004 2005
0 World GDP 5.590000e+13 5.810000e+13
1 World Health 5.590000e+13 5.810000e+13
086 Zimbabwe GDP per capita 8.681564e+02 8.082944e+02
089 Zimbabwe Population 1.277751e+07 1.294003e+07
所以你马上就会注意到每个国家都有多个指标。你知道吗
我要做的是从当前的两个指标中创建一个新的指标。为每个独特的国家创造。你知道吗
for i in series(gdp['Country.Name']):
gdp['Military Spending'] = 100 / gdp['Military percent of GDP'] *
gdp['GDP']
它给了我这个错误信息:
NameError Traceback (most recent call last)
<ipython-input-37-d817ea1522fc> in <module>()
----> 1 for i in series(gdp1['Country.Name']):
2 gdp1['Military Spending'] = 100 / gdp1['Military percent of GDP'] *
gdp1['GDP']
NameError: name 'series' is not defined
我怎样才能让这个系列发挥作用?我也试过简单的方法
for i in gdp['Country.Name']
但仍然会收到错误消息。你知道吗
救命啊!你知道吗
假设您有以下输入
Dataframe
(请注意,在您的示例中数据Military percent of GDP
不存在):然后可以分别为
GDP
和Military percent of GDP
使用2004
和2005
中的数据创建辅助数据帧df_gdp
和df_mpgdp
。然后您可以创建df_msp
,其中将包含名为Military Spending
的新Indicator.Name
,最后将其结果附加到原始Dataframe
。注意,在某些情况下,我们需要reset_index
,以确保使用预期的索引完成计算。你知道吗以下代码适用于您的目标:
最后输出
Dataframe
将导致:相关问题 更多 >
编程相关推荐