Reputation: 2572
I have a pd.DataFrame
that looks like this:
In [30]: df
Out[30]:
DATES UID A
0 2014-01-01 1 False
1 2014-01-02 2 False
2 2014-01-03 3 True
3 2014-01-04 4 True
4 2014-01-05 5 False
5 2014-01-06 6 True
6 2014-01-07 1 False
7 2014-01-08 2 False
8 2014-01-09 3 False
9 2014-01-10 2 False
10 2014-01-11 3 False
11 2014-01-12 4 False
12 2014-01-13 5 False
13 2014-01-14 3 False
14 2014-01-15 1 False
and I would like to find a way to:
The desired output would look like this:
In [30]: df
Out[30]:
DATES UID A
0 2014-01-03 3 True
1 2014-01-04 4 True
2 2014-01-06 6 True
3 2014-01-09 3 False
4 2014-01-11 3 False
5 2014-01-12 4 False
6 2014-01-14 3 False
Any ideas very much appreciated, thanks!
Upvotes: 0
Views: 88
Reputation: 862431
It looks like need first sort_values
and then filter
:
df.sort_values(by='DATES', inplace=True)
df = df.groupby('UID', sort=False).filter(lambda x: x.A.iloc[0] == True)
print (df)
DATES UID A
2 2014-01-03 3 True
3 2014-01-04 4 True
5 2014-01-06 6 True
8 2014-01-09 3 False
10 2014-01-11 3 False
11 2014-01-12 4 False
13 2014-01-14 3 False
Upvotes: 1