I have a dataframe df
as follows
Date Group Value Duration
2018-01-01 A 20 30
2018-02-01 A 10 60
2018-03-01 A 25 88 <-----Last row for Group A
2018-01-01 B 15 180
2018-02-01 B 30 210
2018-03-01 B 25 238 <-----Last row of Group B
I want to drop the Group
A because it hax max duration less than 90. Or in other words, considering the last row of each Group
, if the Duration
value is less than 90, we omit that group. So my resultant data frame should look like
Date Group Value Duration
2018-01-01 B 15 180
2018-02-01 B 30 210
2018-03-01 B 25 240
To achieve this, my approach is as follows:
df_f = []
for k,v in df.groupby(['Group']):
v_f = v[max(v['Duration'])>=90]
df_f.append(v_f)
The above code snippet is throwing an error as KeyError: False
Am I missing out anything here?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…