time loc
0 2014-12-08 18 ad
1 2014-12-09 12 as
2 2014-12-12 12 xs
处理为
time loc
0 1 ad
1 2 as
2 5 xs
其中 "2014-12-08 18"
代表 14 年 12 月 8 日的 18 点,想把时间这列更新为从12 月 8 日算起的第X天,应该如何操作?
1
aaronzjw 2016-09-01 23:49:29 +08:00
import time 应该可以吧
|
2
20150517 2016-09-02 03:58:10 +08:00 via iPhone
apply
|
3
wickila 2016-09-02 09:39:53 +08:00 1
import pandas as pd
import datetime def date2day(x): sd = datetime.datetime.strptime('2014-12-08 0', '%Y-%M-%d %H') d = datetime.datetime.strptime(x['time'], '%Y-%M-%d %H') x['time'] = (d - sd).days + 1 return x df = pd.DataFrame([['2014-12-08 18', 'ad'], ['2014-12-09 12', 'as'], ['2014-12-12 12', 'xs']], columns=['time', 'loc']) df = df.apply(date2day, axis=1) print df |
4
xixijun 2016-09-02 10:29:28 +08:00
df['date_diff']=df['time'].diff().fillna(0)+pd.Timedelta('1 days')
|
9
weimao 2018-04-09 17:42:39 +08:00
td = _df['time']
time = pd.to_datetime(td) start = pd.datetime(2014, 12, 8) day = time - start _df['time'] = day.dt.days |