简体   繁体   中英

Pandas Groupby Min and Max of last N rows of a column

A time series data has 3 columns apart from index, which is time

indexTime,A,B,C

I want to list all As that have last 10 B and C +ve

This means I have to do a

groupby('A')

and then have an AND condition for

last N rows of B.min() > 0 AND last N rows of C.min() > 0

How do I do it ?

09:00,ABC,1,1
09:00,XYZ,15,2
09:01,ABC,2,4
09:01,XYZ,1,2
09:02,ABC,-1,2
09:02,XYZ,1,7
09:03,ABC,3,5
09:03,XYZ,5,2

let us say last 3 in this case XYZ would satisfy the condition as it has both B and C column last 3 rows positive where as ABC does not have all last 3 rows positive

Column B of ABC 09:02 is -1 so it would fail the test even though column C of ABC is all positive. But because of AND condition it would fail

Thus for the condition output would be XYZ as only that satisfies the condition

Use groupby with tail and all for check all True s:

a = df.groupby('A').apply(lambda x: (x.tail(3) > 0).all(1))
print (a)
     09:01  09:02  09:03
A                       
ABC   True  False   True
XYZ   True   True   True

b = a.index[a.all(1)]
print (b)
Index(['XYZ'], dtype='object', name='A')

print (a)
A        ABC   XYZ
09:01   True  True
09:02  False  True
09:03   True  True

b = a.columns[a.all()].tolist()
print (b)
['XYZ']

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