【问题标题】:Pandas: select rows were a column has lists that are not empty [duplicate]熊猫:选择行是一列的列表不为空[重复]
【发布时间】:2018-10-21 04:27:14
【问题描述】:

我有一个数据框,其中有一列值是列表。我需要选择这些列表不为空的行:

import pandas as pd
data = [('words', ['foo', 'bar', 'baz', 'foobar', 'helter', 'skelter']),
         ('counts', [[1,2,3], [], [5,8], [13,21,34,55], [89], [] ])
         ]
df = pd.DataFrame.from_items(data)
df

Output:
    words   counts
0   foo     [1, 2, 3]
1   bar         []
2   baz         [5, 8]
3   foobar  [13, 21, 34, 55]
4   helter  [89]
5   skelter     []

选择这种方式失败:

df[df['counts'] != []]

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-db8067c8ac7b> in <module>()
----> 1 df[df['counts'] != []]

/Users/user/usr/anaconda/lib/python3.5/site-packages/pandas/core/ops.py in wrapper(self, other, axis)
    859 
    860             with np.errstate(all='ignore'):
--> 861                 res = na_op(values, other)
    862             if is_scalar(res):
    863                 raise TypeError('Could not compare %s type with Series' %

/Users/user/usr/anaconda/lib/python3.5/site-packages/pandas/core/ops.py in na_op(x, y)
    763 
    764         if is_object_dtype(x.dtype):
--> 765             result = _comp_method_OBJECT_ARRAY(op, x, y)
    766         else:
    767 

/Users/user/usr/anaconda/lib/python3.5/site-packages/pandas/core/ops.py in _comp_method_OBJECT_ARRAY(op, x, y)
    741             y = y.values
    742 
--> 743         result = lib.vec_compare(x, y, op)
    744     else:
    745         result = lib.scalar_compare(x, y, op)

pandas/_libs/lib.pyx in pandas._libs.lib.vec_compare (pandas/_libs/lib.c:14284)()

ValueError: Arrays were different lengths: 6 vs 0

df.query 之类的其他内容在这种情况下也不起作用。任何想法如何解决这个问题?为什么无法将 pandas 单元格值与空列表进行比较?

【问题讨论】:

  • df[df.counts.astype(bool)] 但是,这是一个骗局。寻找副本。
  • pandas 通常不能很好地处理可变长度数组的列。

标签: python pandas select


【解决方案1】:

这是一种很老套但有效的方法:

df[ df.counts.str.len() > 0 ]

【讨论】:

    猜你喜欢
    • 2020-08-01
    • 2019-04-02
    • 2019-09-22
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2021-07-15
    • 2013-02-03
    相关资源
    最近更新 更多