import pandas as pd
import datetime
df=pd.read_csv('a.csv')
diff=[]
df['date']=pd.to_datetime(df['date'])
for i in range(0,len(df['date'])):
localdf=df.loc[(df['date']==df['date'][i] -datetime.timedelta(days=5))&(df['id'] == df['id'][i])]
if localdf.empty:
diff.append(0)
else:
localdf=localdf.reset_index()
diff.append(df['value'][i]-localdf['value'][0])
df['diff']=diff
1条答案
按热度按时间dsekswqp1#
我创建了一个Excel文件:
使用代码:
给出: