获取每个熊猫中每个组的列中包含特定值的行

问题描述 投票:0回答:1

想知道您是否可以为此提供一些指导-我仍在研究this data,这是我正在尝试做的事情:-对于每个分组的“国家或地区”,我试图获取包含2016年“数量”和2011年“数量”的行。但是,似乎有些国家没有2016年或2011年的行。问题是执行以下代码时出现错误:


for c in grp['Country or Area'].unique():
  deltafiveyrs.append(grp[(grp['Year'] == 2016.0) & (grp['Country or Area'] == c)]['Quantity'] -  grp[(grp['Year'] == 2011.0) & (grp['Country or Area'] == c)]['Quantity'])

我收到的错误消息是:

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:5: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
  """
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-30-90579ab30ed1> in <module>()
      3 
      4 for c in grp['Country or Area'].unique():
----> 5   deltafiveyrs.append(grp[(grp['Year'] == 2016.0) & (grp['Country or Area'] == c)]['Quantity'] -  grp[(grp['Year'] == 2011.0) & (grp['Country or Area'] == c)]['Quantity'])
      6 
      7 

/usr/local/lib/python3.6/dist-packages/pandas/core/base.py in __getitem__(self, key)
    266         else:
    267             if key not in self.obj:
--> 268                 raise KeyError("Column not found: {key}".format(key=key))
    269             return self._gotitem(key, ndim=1)
    270 

KeyError: 'Column not found: False'

有人知道发生了什么吗?是否应将“年”列中的值从float更改为int?如何处理2011/2016年没有价值的群体的最佳方法是什么?

非常感谢

python pandas int data-cleaning isnull
1个回答
0
投票

请先研究并清理数据,然后再进行处理。少数列(忽略“数量”脚注)具有nan值。因此,让我们删除这些行,然后继续进行。我在jupyter笔记本中进行了尝试,该笔记本在2011年和2016年给出了具有数量的国家的数据框。

cdf = df[['Country or Area', 'Commodity - Transaction', 'Year', 'Unit', 'Quantity']]
cdf.dropna()
cdf[(cdf['Year']== 2011) | (cdf['Year']== 2016)]
© www.soinside.com 2019 - 2024. All rights reserved.