我有一个看起来像这样的DataFrame。
time daysToExp
0 2020-08-11 13:53:57.083388 0
1 2020-08-11 13:53:57.083388 1
2 2020-08-11 13:53:57.083388 3
3 2020-08-11 13:53:57.083388 4
4 2020-08-11 13:53:57.083388 8
我想将daysToExp列添加到time列,并以类似这样的结果结束。
time daysToExp Date
0 2020-08-11 13:53:57.083388 0 2020-08-11 13:53:57.083388
1 2020-08-11 13:53:57.083388 1 2020-08-12 13:53:57.083388
2 2020-08-11 13:53:57.083388 3 2020-08-14 13:53:57.083388
3 2020-08-11 13:53:57.083388 4 2020-08-15 13:53:57.083388
4 2020-08-11 13:53:57.083388 8 2020-08-19 13:53:57.083388
我找到的最接近的命令是df['Date'] = df['time'] + pd.DateOffset(days=3)
,但它不能在DataFrame中完成整个列。我该怎么办?
答案 0 :(得分:1)
您可以使用pd.to_timedelta
:
df['time'] + pd.to_timedelta(df['daysToExp'], unit='D')
或等效地:
df['time'] + pd.to_timedelta('1D') * df['daysToExp']