简体   繁体   English

筛选 pandas dataframe 行,其中具有列 A 和值 X 的特定行具有列 B,其值 Y 大于参数 Z

[英]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 72154 X1 X1 0.998429 0.998429
72155 72155 X2 X2 0.584253 0.584253
72156 72156 X3 X3 0.797648 0.797648
72157 72157 X2 X2 0.981707 0.981707
72158 72158 X1 X1 0.698844 0.698844
72159 72159 X3 X3 0.987943 0.987943
72160 72160 X1 X1 0.797648 0.797648
72161 72161 X3 X3 0.984621 0.984621
72162 72162 X2 X2 0.221968 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.

相关问题 过滤熊猫数据框中的行,其中列中的值大于 x 或 NaN - Filter for rows in pandas dataframe where values in a column are greater than x or NaN Pandas dataframe,最大值,其中列值大于变量 - Pandas dataframe, max value where column value is greater than variable 熊猫数据框:如果A,B或C列中的行包含“ x”或“ y”,则将“ z”写入新列 - Pandas Dataframe: if row in column A, B or C contains “x” or “y”, write “z” to new column 更新Pandas DataFrame中B列的值为C的A列 - Update Column A where Column B has Value C in Pandas DataFrame Pandas, groupby 列值大于 x - Pandas, groupby where column value is greater than x 如何检查 pandas dataframe 中的列的值是否大于上述值。 如果该列的值作为列表 - How to check if a column in pandas dataframe has a value greater than mentioned value . If The column has values as lists Python Pandas如果B列中的值等于[X,Y,Z],则将A列替换为“ T” - Python Pandas If value in column B = equals [X, Y, Z] replace column A with “T” 如何过滤pandas数据框中的行,其中列的值等于列表的某个值 - How to filter rows in pandas dataframe where a column has a value equal to some value of a list Python pandas 打印值,其中 column = X 和 row = Y - Python pandas print value where column = X and row = Y Pandas / Python = Function 通过将 Y 列与在 X 中具有值的另一行匹配来替换 X 列中的 NaN 值 - Pandas / Python = Function that replaces NaN value in column X by matching Column Y with another row that has a value in X
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM