简体   繁体   English

Python2.7:根据 groupby 中的条件从数据框中过滤出组

[英]Python2.7: FIlter out group from dataframe based on condition in groupby

I have a dataframe and I would like to filter the dataframe further to only include a group whose rows do not have a certain value in a column我有一个数据框,我想进一步过滤该数据框以仅包含其行在列中没有特定值的组

For eg, in the dataframe, since hamilton has an overtake in lap3 of his stint 1, I want to remove ALL of hamilton's stint 1 laptime records from the dataframe below.例如,在数据框中,由于汉密尔顿在他的第 1 阶段的第 3 圈超车,我想从下面的数据框中删除汉密尔顿的所有第 1 圈记录。

I thought of doing a groupby and then a get group,iterate through each row in the group, detect non-null value in the "clear lap?"我想到做一个groupby然后一个get group,遍历组中的每一行,在“clear lap”中检测非空值? column, and label "yes" in a new column for all rows in the groupby, then filter out the group.列,并在新列中为 groupby 中的所有行标记“是”,然后过滤掉该组。

Is there a faster way of subsetting the dataframe?有没有更快的方法来设置数据帧的子集?

Dataframe:数据框:

    name                   driverRef stint  tyre      lap   pos     clear lap?
0   Australian Grand Prix   vettel  1.0     Super soft  2   1        NaN
1   Australian Grand Prix   vettel  1.0     Super soft  3   1        NaN
2   Australian Grand Prix   vettel  1.0     Super soft  4   1        NaN
3   Australian Grand Prix   ham     1.0     Super soft  2   3        NaN
4   Australian Grand Prix   ham     1.0     Super soft  3   2        overtook
5   Australian Grand Prix   ham     1.0     Super soft  4   2        NaN

I believe you need get all groups by filtering and then filter again by isin :我相信您需要通过过滤获取所有组,然后通过isin再次过滤:

Notice: Thank you, @Vivek Kalyanarangan for improvement by unique .注意:谢谢@Vivek Kalyanarangan 通过unique改进。

a = df.loc[df['clear lap?'].notnull(), 'driverRef'].unique()
print (a)
['ham']

df = df[~df['driverRef'].isin(a)]
print (df)
                    name driverRef  stint        tyre  lap  pos clear lap?
0  Australian Grand Prix    vettel    1.0  Super soft    2    1        NaN
1  Australian Grand Prix    vettel    1.0  Super soft    3    1        NaN
2  Australian Grand Prix    vettel    1.0  Super soft    4    1        NaN

Another solution, slowier:另一个解决方案,速度较慢:

df = df[df['clear lap?'].isnull().groupby(df['driverRef']).transform('all')]

Or slowiest:或者最慢:

df = df.groupby('driverRef').filter(lambda x: x['clear lap?'].isnull().all())

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

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