[英]Filter pandas dataframe rows where a specific row with column A and value X has column B with value Y greater than a parameter Z
I wonder if there's a simpler way to filter a pandas DataFrame rows where a specific row with column A and value X has column B with value Y greater than a parameter Z.我想知道是否有更简单的方法来过滤 pandas DataFrame 行,其中具有列 A 和值 X 的特定行具有列 B,其值 Y 大于参数 Z。
For example:例如:
A![]() |
B![]() |
|
---|---|---|
72154 ![]() |
X1 ![]() |
0.998429 ![]() |
72155 ![]() |
X2 ![]() |
0.584253 ![]() |
72156 ![]() |
X3 ![]() |
0.797648 ![]() |
72157 ![]() |
X2 ![]() |
0.981707 ![]() |
72158 ![]() |
X1 ![]() |
0.698844 ![]() |
72159 ![]() |
X3 ![]() |
0.987943 ![]() |
72160 ![]() |
X1 ![]() |
0.797648 ![]() |
72161 ![]() |
X3 ![]() |
0.984621 ![]() |
72162 ![]() |
X2 ![]() |
0.221968 ![]() |
I've managed to get it done with this code:我设法用这段代码完成了它:
import pandas as pd
Z = 0.8
mask1 = (df.A.isin(['X1', 'X2']))
mask2 = (df.B > Z)
mask3 = (df.A == 'X3')
sub_df_x1_x2 = df[mask1 & mask2]
sub_df_x3 = df[mask3]
final_df = pd.concat([sub_df_x1_x2, sub_df_x3])
But I don't believe this is the cleanest or best way to do it.但我不认为这是最干净或最好的方法。 Do you guys have any idea?
你们有什么想法吗? I was thinking about something like this, but I was not able to get it to work.
我在想这样的事情,但我无法让它发挥作用。
mask1 = (df.A.isin(['X1', 'X2']) & df.B > Z)
# or
mask1 = (df.A.isin(['X1', 'X2'])[B] > Z)
You could chain the two with |
你可以用
|
链接这两个: :
out = df[(df.A.isin(['X1', 'X2']) & (df.B > Z)) | (df.A == 'X3')]
or using the definitions you already have:或使用您已有的定义:
out = df[(mask1 & mask2) | mask3]
Output: Output:
A B
72154 X1 0.998429
72156 X3 0.797648
72157 X2 0.981707
72159 X3 0.987943
72161 X3 0.984621
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.