繁体   English   中英

使用包含空格的列名称或使用包含空格的列名称的drop方法查询Pandas DataFrame

[英]Querying Pandas DataFrame with column name that contains a space or using the drop method with a column name that contains a space

我希望使用pandas根据列名(包含空格)和单元格值删除行。 我已经尝试了各种方法来实现这一点(drop和query方法),但由于名称中的空格,我似乎失败了。 有没有办法使用其中有空格的名称查询数据,或者我是否需要先清理所有空格?

数据以csv文件的形式

Date,"price","Sale Item"
2012-06-11,1600.20,item1
2012-06-12,1610.02,item2
2012-06-13,1618.07,item3
2012-06-14,1624.40,item4
2012-06-15,1626.15,item5
2012-06-16,1626.15,item6
2012-06-17,1626.15,item7

尝试例子

df.drop(['Sale Item'] != 'Item1')
df.drop('Sale Item' != 'Item1')
df.drop("'Sale Item'] != 'Item1'")

df.query('Sale Item' != 'Item1')
df.query(['Sale Item'] != 'Item1')
df.query("'Sale Item'] != 'Item1'")

大多数情况下收到错误

ImportError: 'numexpr' not found. Cannot use engine='numexpr' for query/eval if 'numexpr' is not installed

如果我正确理解了您的问题,也许您只需应用以下过滤器:

df = df[df['Sale Item'] != 'item1']

返回:

         Date    price Sale Item
1  2012-06-12  1610.02     item2
2  2012-06-13  1618.07     item3
3  2012-06-14  1624.40     item4
4  2012-06-15  1626.15     item5
5  2012-06-16  1626.15     item6
6  2012-06-17  1626.15     item7

文档中可以看出 -

DataFrame.drop(labels,axis = 0,level = None,inplace = False,errors ='raise')

返回删除了请求轴中的标签的新对象

DataFrame.drop()获取要删除的行的index ,而不是条件。 因此你很可能需要像 -

df.drop(df.ix[df['Sale Item'] != 'item1'].index)

请注意,这会丢弃符合条件的行,因此结果将是不符合条件的行,如果您想要相反,则可以在条件之前使用~运算符来否定它。

但这看起来有点过分,使用布尔索引来获取所需的行会更容易(如另一个答案中所示)。


演示 -

In [20]: df
Out[20]:
         Date    price Sale Item
0  2012-06-11  1600.20     item1
1  2012-06-12  1610.02     item2
2  2012-06-13  1618.07     item3
3  2012-06-14  1624.40     item4
4  2012-06-15  1626.15     item5
5  2012-06-16  1626.15     item6
6  2012-06-17  1626.15     item7

In [21]: df.drop(df.ix[df['Sale Item'] != 'item1'].index)
Out[21]:
         Date   price Sale Item
0  2012-06-11  1600.2     item1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM