我的数据框abc=20 # assuming you got this from the input file
val=1 # assuming you also got this from the input file
varName="day_${abc}_id"
command="${varName}=${val}"
eval $command
# now print to output file as you have stated in the comments
outputFile=output.txt # randomly-chosen name
command="echo $varName = $val > $outputFile"
eval $command
的最后一列如下所示:
df
问题:
我想创建一个名为 ... Days till Service
0 ... 0
1 ... 28
2 ... 7
3 ... 54
4 ... 0
5 ... 6
6 ... 28
7 ... 0
8 ... 16
9 ... 200
的新列,我可以将Predicted Service Date
添加到今天的日期。我怎么能这样做?
理想输出:
如果今天的日期为Days till Service
,则16/04/2018
应如下所示:
df
答案 0 :(得分:2)
使用pd.to_timedelta
,您可以将df['Days till Service']
转换为timedeltas,然后将其添加到今天的日期:
df['Predicted Service Date'] = \
pd.to_datetime('today') + pd.to_timedelta(df['Days till Service'], unit='D')
答案 1 :(得分:0)
您要做的是定义日期偏移,然后将其添加到DateTime。熊猫接受datetime()
和Timestamp
也可以随意使用。
您可以使用offsets
定义Day
,首先将偏移系列转换为Day
偏移类,然后将起始点的DateTime添加到其中。
from pandas.tseries.offsets import Day
from datetime import datetime
df['Predicted Service Date'] = df['Days till Service'].apply(Day) + datetime(2018, 4, 17) #Or What everdate you would like