pandas:使用运算符链接过滤 DataFrame 的行 [英] pandas: filter rows of DataFrame with operator chaining
问题描述
pandas
中的大多数操作都可以通过操作符链来完成(groupby
、aggregate
、apply
等),但我发现过滤行的唯一方法是通过普通括号索引
Most operations in pandas
can be accomplished with operator chaining (groupby
, aggregate
, apply
, etc), but the only way I've found to filter rows is via normal bracket indexing
df_filtered = df[df['column'] == value]
这并不吸引人,因为它需要我将 df
分配给一个变量,然后才能对其值进行过滤.有没有更像下面的内容?
This is unappealing as it requires I assign df
to a variable before being able to filter on its values. Is there something more like the following?
df_filtered = df.mask(lambda x: x['column'] == value)
推荐答案
我不完全确定你想要什么,你的最后一行代码也没有帮助,但无论如何:
I'm not entirely sure what you want, and your last line of code does not help either, but anyway:
链接"过滤是通过链接"布尔索引中的条件来完成的.
"Chained" filtering is done by "chaining" the criteria in the boolean index.
In [96]: df
Out[96]:
A B C D
a 1 4 9 1
b 4 5 0 2
c 5 5 1 0
d 1 3 9 6
In [99]: df[(df.A == 1) & (df.D == 6)]
Out[99]:
A B C D
d 1 3 9 6
如果要链接方法,可以添加自己的掩码方法并使用该方法.
If you want to chain methods, you can add your own mask method and use that one.
In [90]: def mask(df, key, value):
....: return df[df[key] == value]
....:
In [92]: pandas.DataFrame.mask = mask
In [93]: df = pandas.DataFrame(np.random.randint(0, 10, (4,4)), index=list('abcd'), columns=list('ABCD'))
In [95]: df.ix['d','A'] = df.ix['a', 'A']
In [96]: df
Out[96]:
A B C D
a 1 4 9 1
b 4 5 0 2
c 5 5 1 0
d 1 3 9 6
In [97]: df.mask('A', 1)
Out[97]:
A B C D
a 1 4 9 1
d 1 3 9 6
In [98]: df.mask('A', 1).mask('D', 6)
Out[98]:
A B C D
d 1 3 9 6
这篇关于pandas:使用运算符链接过滤 DataFrame 的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!