我有 df 喜歡跟隨一個(gè)customer movement dateA buy 2019/5/4 A inquiry 2020/7/1A cancel 2020/8/1B buy 2019/6/1B cancel 2020/8/1我想追查每個(gè)客戶movement之前cancel首先,分組依據(jù)customerA buy 2019/5/4 A inquiry 2020/7/1A cancel 2020/8/1然后我想得到cancel dateA cancel 2020/8/1然后,我想previous movement在取消之前得到in 1 year。customer movement dateA inquiry 2020/7/1A cancel 2020/8/1在那之后,我想在每個(gè)customers所以我想要的結(jié)果如下customer movement dateA inquiry 2020/7/1A cancel 2020/8/1B cancel 2020/8/1有什么辦法可以做到這一點(diǎn)?這太復(fù)雜了,我無法處理這樣的程序..
1 回答

Qyouu
TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
首先將列轉(zhuǎn)換為日期時(shí)間并創(chuàng)建僅過濾行的cancel
系列DataFrame.set_index
:
df['date']?=?pd.to_datetime(df['date']) s?=?df[df['movement'].eq('cancel')].set_index('customer')['date']
然后按Series.map
年減去 1 年進(jìn)行映射,并按indate
過濾列的較少值:Series.lt
boolean indexing
df = df[df['customer'].map(s.sub(pd.DateOffset(years=1))).lt(df['date'])]
print (df)
? customer movement? ? ? ?date
1? ? ? ? A? inquiry 2020-07-01
2? ? ? ? A? ?cancel 2020-08-01
4? ? ? ? B? ?cancel 2020-08-01
添加回答
舉報(bào)
0/150
提交
取消