具有布尔值和整数的数据帧的 Pandas 条件子集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33191738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:03:46  来源:igfitidea点击:

Pandas conditional subset for dataframe with bool values and ints

pythonpandasconditionalsubset

提问by Christopher Jenkins

I have a dataframe with three series. Column A contains a group_id. Column B contains True or False. Column C contains a 1-n ranking (where n is the number of rows per group_id).

我有一个包含三个系列的数据框。A 列包含 group_id。B 列包含 True 或 False。C 列包含 1-n 排名(其中 n 是每个 group_id 的行数)。

I'd like to store a subset of this dataframe for each row that:

我想为每一行存储这个数据帧的一个子集:

1) Column C == 1
OR
2) Column B == True

The following logic copies my old dataframe row for row into the new dataframe:

以下逻辑将我的旧数据帧行复制到新数据帧中:

new_df = df[df.column_b | df.column_c == 1]

回答by Fabio Lamanna

IIUC, starting from a sample dataframe like:

IIUC,从示例数据帧开始,例如:

A,B,C
01,True,1
01,False,2
02,False,1
02,True,2
03,True,1

you can:

你可以:

df = df[(df['C']==1) | (df['B']==True)]

which returns:

返回:

   A      B  C
0  1   True  1
2  2  False  1
3  2   True  2
4  3   True  1

回答by Zero

You've couple of methods for filtering, and performance varies based on size of your data

您有几种过滤方法,性能因数据大小而异

In [722]: df[(df['C']==1) | df['B']]
Out[722]:
   A      B  C
0  1   True  1
2  2  False  1
3  2   True  2
4  3   True  1

In [723]: df.query('C==1 or B==True')
Out[723]:
   A      B  C
0  1   True  1
2  2  False  1
3  2   True  2
4  3   True  1

In [724]: df[df.eval('C==1 or B==True')]
Out[724]:
   A      B  C
0  1   True  1
2  2  False  1
3  2   True  2
4  3   True  1