简体   繁体   English

熊猫数据框按条件放置行

[英]Pandas dataframe drop rows by condition

I use Pandas. 我用熊猫。 I have DataFrame. 我有DataFrame。

I want to delete rows according to the same conditions. 我想根据相同的条件删除行。

bool_mask = (df['a'] == 'aaa') & \
            (df['b'] == '') & \
            (df['c'] == '') & \
            (df['d'] == '') & \
            (df['e'] == '') & \
            (df['f'] == '') & \
            (df['m'] == '') & \
            (df['n'] == '') & \
            (df['w'] == '')

df = df[~bool_mask]

Please tell me if there is a prettier / faster solution? 请告诉我是否有更漂亮/更快的解决方案?

I think you may use all on combining columns compared to '' 我想你可以使用all的比列相结合''

bool_mask = (df['a'] == 'aaa') & \
            (df[['b', 'c', 'd', 'e', 'f', 'm', 'n', 'w']] == '').all(1)

df = df[~bool_mask]

We can cut it down as below , find all other columns which equal to '' and columns a equal to 'aaa' is the target : 我们可以按如下所示将其缩减,找到其他所有等于''的列和等于aa的列a作为目标:

# make whole df equal to '' then sum all of them by row 
# then we at least need 8 blank 
# then apply the 2nd condition df.a should be equal to 'aaa' 
m=(df=='').sum(1).eq(8)&df.a.eq('aaa')

Or 要么

m=df.drop('a',1).eq('').all(1)&df.a.eq('aaa')

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

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