[英]filter and extend time-series pandas dataframe
這個問題是這個問題的附加問題: filter multi-indexed grouped pandas dataframe
我想獲取date
之后value
開始大於零的timestamp
,作為每個單獨id
的新列new_date
示例輸入數據:
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
2 2001-01-01 2001-05-01 1
2 2001-10-01 2001-05-01 0
2 2001-10-02 2001-05-01 0
2 2001-10-03 2001-05-01 0
2 2001-10-04 2001-05-01 1
想要的 Output 數據示例:
id timestamp date value new_date
1 2001-01-01 2001-05-01 1 2001-10-02
1 2001-10-01 2001-05-01 0 2001-10-02
1 2001-10-02 2001-05-01 1 2001-10-02
1 2001-10-03 2001-05-01 0 2001-10-02
1 2001-10-04 2001-05-01 1 2001-10-02
2 2001-01-01 2001-05-01 1 2001-10-04
2 2001-10-01 2001-05-01 0 2001-10-04
2 2001-10-02 2001-05-01 0 2001-10-04
2 2001-10-03 2001-05-01 0 2001-10-04
2 2001-10-04 2001-05-01 1 2001-10-04
Simplier solution working also if some group has no match is first filter DataFrame
chained mask for greater like date
by Series.gt
with bitwise AND
same for 0
, then remove duplicates by DataFrame.drop_duplicates
, create Series
and last use Series.map
:
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']) & df['value'].gt(0)
s = df[m].drop_duplicates('id').set_index('id')['timestamp']
df['new_date'] = df['id'].map(s)
print (df)
id timestamp date value new_date
0 1 2001-01-01 2001-05-01 1 2001-10-02
1 1 2001-10-01 2001-05-01 0 2001-10-02
2 1 2001-10-02 2001-05-01 1 2001-10-02
3 1 2001-10-03 2001-05-01 0 2001-10-02
4 1 2001-10-04 2001-05-01 1 2001-10-02
5 2 2001-01-01 2001-05-01 1 2001-10-04
6 2 2001-10-01 2001-05-01 0 2001-10-04
7 2 2001-10-02 2001-05-01 0 2001-10-04
8 2 2001-10-03 2001-05-01 0 2001-10-04
9 2 2001-10-04 2001-05-01 1 2001-10-04
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.