简体   繁体   中英

filter time-series pandas dataframe by column value

This question is an addon to this question: filter multi-indexed grouped pandas dataframe

I want all data (time-wise) after date starting from the first value greater zero. (applied for every id )

Example Input data:

id  timestamp   date        value
1   2001-01-01  2001-05-01  1
1   2001-10-01  2001-05-01  0
1   2001-10-02  2001-05-01  1
1   2001-10-03  2001-05-01  0
1   2001-10-04  2001-05-01  1

Wanted Output data example:

id  timestamp   date        value
1   2001-10-02  2001-05-01  1
1   2001-10-03  2001-05-01  0
1   2001-10-04  2001-05-01  1

First filter by Series.gt by another column, then create GroupBy.cumsum , filter for greater like 0 and last add removed values by DataFrame.reindex :

df['timestamp'] = pd.to_datetime(df['timestamp'])
df['date'] = pd.to_datetime(df['date'])
df = df.sort_values(['id','timestamp'])

m = df['timestamp'].gt(df['date'])
m1 = df[m].groupby('id')['value'].cumsum().gt(0).reindex(df.index, fill_value=False)
df = df[m1]
print (df)
   id  timestamp       date  value
2   1 2001-10-02 2001-05-01      1
3   1 2001-10-03 2001-05-01      0
4   1 2001-10-04 2001-05-01      1

Another idea with replace column by Series.where :

df['timestamp'] = pd.to_datetime(df['timestamp'])
df['date'] = pd.to_datetime(df['date'])
df = df.sort_values(['id','timestamp'])

m = df['timestamp'].gt(df['date'])
m1 = df.assign(value = df['value'].where(m, 0)).groupby('id')['value'].cumsum().gt(0)
df = df[m1]
print (df)
   id  timestamp       date  value
2   1 2001-10-02 2001-05-01      1
3   1 2001-10-03 2001-05-01      0
4   1 2001-10-04 2001-05-01      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