我有 df 喜欢跟随一个customer movement dateA buy 2019/5/4 A inquiry 2020/7/1A cancel 2020/8/1B buy 2019/6/1B cancel 2020/8/1我想追查每个客户movement之前cancel首先,分组依据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在那之后,我想在每个customers所以我想要的结果如下customer movement dateA inquiry 2020/7/1A cancel 2020/8/1B cancel 2020/8/1有什么办法可以做到这一点?这太复杂了,我无法处理这样的程序..
1 回答
Qyouu
TA贡献1786条经验 获得超11个赞
首先将列转换为日期时间并创建仅过滤行的cancel
系列DataFrame.set_index
:
df['date'] = pd.to_datetime(df['date']) s = df[df['movement'].eq('cancel')].set_index('customer')['date']
然后按Series.map
年减去 1 年进行映射,并按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
添加回答
举报
0/150
提交
取消