【问题标题】:Filtering by condition in Python dataset在 Python 数据集中按条件过滤
【发布时间】:2017-05-26 11:36:57
【问题描述】:

我在 Phyton3 中对 stata 文件进行排序操作时遇到了困难:我被要求只将没有孩子的家庭排除在数据集/表之外:

我使用过滤条件将这些行从表中过滤出来:

filtering_condition = df["kids"] > 0

df_nokids = df.loc[filtering_condition,"kids"]

然而,这给了我一个未知的错误:

KeyError                                  Traceback (most recent call last)
/opt/anaconda/anaconda3/lib/python3.5/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
1944             try:
-> 1945                 return self._engine.get_loc(key)
   1946             except KeyError:

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item     (pandas/hashtable.c:12322)()

KeyError: 'kids'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-321-e72cd8a67065> in <module>()
      1 #keep only the households without kids and use this dataset for the   rest of the assignment
----> 2 filtering_condition = df["kids"] > 0
      3 df_nokids = df.loc[filtering_condition,"kids"]

/opt/anaconda/anaconda3/lib/python3.5/site-packages/pandas/core/frame.py in  __getitem__(self, key)
   1995             return self._getitem_multilevel(key)
   1996         else:
-> 1997             return self._getitem_column(key)
   1998 
   1999     def _getitem_column(self, key):

/opt/anaconda/anaconda3/lib/python3.5/site-packages/pandas/core/frame.py in  _getitem_column(self, key)
   2002         # get column
   2003         if self.columns.is_unique:
-> 2004             return self._get_item_cache(key)
   2005 
   2006         # duplicate columns & possible reduce dimensionality

/opt/anaconda/anaconda3/lib/python3.5/site-packages/pandas/core/generic.py    in _get_item_cache(self, item)
   1348         res = cache.get(item)
   1349         if res is None:
-> 1350             values = self._data.get(item)
   1351             res = self._box_item_values(item, values)
   1352             cache[item] = res

/opt/anaconda/anaconda3/lib/python3.5/site-packages/pandas/core/internals.py     in get(self, item, fastpath)
   3288 
   3289             if not isnull(item):
-> 3290                 loc = self.items.get_loc(item)
   3291             else:
   3292                 indexer = np.arange(len(self.items))   [isnull(self.items)]

 /opt/anaconda/anaconda3/lib/python3.5/site-packages/pandas/indexes/base.py    in get_loc(self, key, method, tolerance)
   1945                 return self._engine.get_loc(key)
   1946             except KeyError:
-> 1947                 return     self._engine.get_loc(self._maybe_cast_indexer(key))
   1948 
   1949         indexer = self.get_indexer([key], method=method,    tolerance=tolerance)

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)()

KeyError: 'kids'

任何解释我做错了什么?

谢谢!

数据文件:

【问题讨论】:

  • 你能提供一个可运行的代码吗?堆栈跟踪显示 Keyerror,这意味着原始数据中不存在“孩子”。如果您可以发布完整的代码会有所帮助。
  • 感谢您的回复,我想您可能是对的,我之前忘记了一步,因为这是我使用的完整代码。我只首先通过以下方式读取 stata 文件: pd.read_stata("data/alcohol.dta") 表格如下所示: , 成人, 儿童, 收入, 消费 0 2 2 758 1 1 2 3 1785 1 2 3 0 1200 1 3 1 0 545 1 4 4 1 547 1

标签: python python-3.x pandas dataset


【解决方案1】:

你的意思是这样的:

df_kids = df[df['kids']>0]

这会选择 'kids' 列不为零的行。

【讨论】:

  • 感谢您的建议。但是,输入此代码会给出类似的错误代码。也许该列可能没有正确编码为孩子?或者我可能需要对 numpy 数组做些什么?
  • 可能您的数据类型不正确(不是整数)。使用 print (df.dtypes) 验证这一点。如果它没有为 'kids' 显示类似 int64 的内容,则必须先将数据类型转换为整数。为此使用 df['kids'] = pd.to_numeric(df['kids'])。
  • 你是对的:它说 x 和 y 都是浮点数。我现在看到这反映了我必须在中间完成的一项任务,即获取数据的描述性统计。我简单地使用了:df.describe() 现在看到这些描述性统计数据不对应于儿童数据我猜描述性代码也是错误的?
  • 好吧,float64 对我上面给出的代码没有问题。但是,user567 正确地指出您有一个密钥错误。这意味着您的列可能没有按照您期望的方式命名。请打印您的 df (或者如果它很长,请 df.head() )。 (最好将其添加到您的问题中)
  • 好吧,其实它不是df。但是一个PD。那会有什么不同吗?
猜你喜欢
  • 2018-07-02
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
  • 1970-01-01
  • 2023-02-10
  • 2016-12-19
  • 1970-01-01
  • 2022-01-22
相关资源
最近更新 更多