简体   繁体   中英

Pandas Dataframe groupby and then filter based on menu or text option

my dataframe looks like below, and my intention is to pandas.groupby on the students name and find out what what activity which they did between 'english' and 'hindi'

 data ={'StudentId':['AAdams','AAdams','AAdams','AAdams','AAdams','AAdams',
                'BBrooks','BBrooks','BBrooks','BBrooks','BBrooks',],

'activity':['came school','english','lunch','hindi','sports','left school','came school','english','read','hindi','left school'],
'month':[11,11,11,11,12,12,12,12,12,1,1]}

pd.DataFrame(data)

StudentId   activity    month
0   AAdams  came school 11
1   AAdams  english 11
2   AAdams  lunch   11
3   AAdams  hindi   11
4   AAdams  sports  12
5   AAdams  left school 12
6   BBrooks came school 12
7   BBrooks english 12
8   BBrooks read    12
9   BBrooks hindi   1
10  BBrooks left school 1

what i have tried so far or i know is

df[df.b.eq('english').groupby(df.StudentId).cumsum()].reset_index(drop=True)

or 

df.groupby('StudentId').apply(lambda x: x.loc[(x.b == 'english').idxmax():,:])
                .reset_index(drop=True)

then take a cut my dataframe and then i can do by below code

df.groupby('StudentId').head(5)

final dataframe or output should look like only the activities between activity=english and activity=hindi

    StudentId   activity    month
1   AAdams  english 11
2   AAdams  lunch   11
3   AAdams  hindi   11
7   BBrooks english 12
8   BBrooks read    12
9   BBrooks hindi   1

Solution if first value per groups is english and second hindi .

Create boolean masks by DataFrameGroupBy.cumsum for first and for second need ordering from back by indexing with [::-1] , last chain mask by & and filter by boolean indexing :

m1 = df['activity'].eq('english').astype(int).groupby(df['StudentId']).cumsum().gt(0)
m2 = df['activity'].eq('hindi').astype(int).iloc[::-1].groupby(df['StudentId']).cumsum().gt(0)

df = df[m1 & m2]
print (df)
  StudentId activity  month
1    AAdams  english     11
2    AAdams    lunch     11
3    AAdams    hindi     11
7   BBrooks  english     12
8   BBrooks     read     12
9   BBrooks    hindi      1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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