如何计算从指定数据到今天的天数?

时间:2017-12-05 10:00:40

标签: pandas

嗨,我有一个数据库的东西,这个:

custId gender firstdate fisrtregion lastdate buytime

  1. abc m yymmdd xws yymmdd
  2. qwe f yymmdd rty yymmdd 1
  3. dfg m yymmdd rew yymmdd 1
  4. 我需要找到从上次购买日期到今天之间的天数。 我该怎么做。

2 个答案:

答案 0 :(得分:0)

我相信你需要:

print (df)
  custId gender firstdate fisrtregion lastdate buytime
0    abc      m    yymmdd         xws   171014       9
1    qwe      f    yymmdd         rty   171116       1
2    dfg      m    yymmdd         rew   171115       1

#convert to datetimes if necessary
df['lastdate'] = pd.to_datetime(df['lastdate'], format='%y%m%d')
print (df)
  custId gender firstdate fisrtregion   lastdate buytime
0    abc      m    yymmdd         xws 2017-10-14       9
1    qwe      f    yymmdd         rty 2017-11-16       1
2    dfg      m    yymmdd         rew 2017-11-15       1

#subtract today with last value in column lastdate
d1 = (pd.to_datetime('today') - df.iloc[-1, df.columns.get_loc('lastdate')]).days
print (d1)
20

#diference with max datetime
d2 = (pd.to_datetime('today') - df['lastdate'].max()).days
print (d2)
19

#subtract today with column lastdate and convert timedeltas to days
df['new'] = (pd.to_datetime('today') - df['lastdate']).dt.days
print (df)
  custId gender firstdate fisrtregion   lastdate buytime  new
0    abc      m    yymmdd         xws 2017-10-14       9   52
1    qwe      f    yymmdd         rty 2017-11-16       1   19
2    dfg      m    yymmdd         rew 2017-11-15       1   20

答案 1 :(得分:0)

这将为您提供从数据库列lastdate到今天的天数

nm = date.today()
df['lastdate'] = df['lastdate'].map(lambda x: datetime.strptime(str(x),'%Y%m%d'))
df['days_from_last_buy'] = date.today() - df['lastdate']
temp = df[['custid','days_from_last_buy']].groupby('custid').agg(max)
temp.columns = ['last_visit_date'] 
df.merge(temp,how='left',left_on='custId',right_index=True)