Python 如何替换数据框中列中的日期?

Python 如何替换数据框中列中的日期?,python,date,Python,Date,我想用特定的日期填充一列。首先,我创建了一列日期,只填充今天的日期。然后,我计算每个位置要替换的日期。索引的格式如下:'%Y-%m-%d' 计算结果良好。这是最后一行行不通的 这是我的代码: for j in range(1, 154): result['LTD_' + str(j)] = dt.date(dt.datetime.now().year,dt.datetime.now().month,dt.datetime.now().day) #creating a column of

我想用特定的日期填充一列。首先,我创建了一列日期,只填充今天的日期。然后,我计算每个位置要替换的日期。索引的格式如下:
'%Y-%m-%d'

计算结果良好。这是最后一行行不通的

这是我的代码:

for j in range(1, 154):
    result['LTD_' + str(j)] = dt.date(dt.datetime.now().year,dt.datetime.now().month,dt.datetime.now().day) #creating a column of dates filled with today day
    for i in range(0, len(result.index)):
        date_selected = result.iloc[[i]].index
        date_month = add_months(date_selected,j)
        delivery_month = date_month.month
        delivery_year = date_month.year
        delivery = dt.date(delivery_year, delivery_month, result[(result.index.month == (delivery_month)) & (result.index.year == delivery_year)].index.day.min())
        delloc = result.index.get_loc(str(delivery))
        ltdloc = delloc - 3
        result.iloc[[i]]['LTD_' + str(j)] = dt.date(result.iloc[[ltdloc]].index.year, result.iloc[[ltdloc]].index.month, result.iloc[[ltdloc]].index.day)
最后一行不能用计算的日期替换今天的日期。什么也没发生。date.replace生成错误。

现在是
ix
,因此需要按位置选择索引:

result.loc[result.index[i], 'LTD_' + str(j)] = ...
或列名称的位置为:

result.iloc[i, result.columns.get_loc('LTD_' + str(j))] = ...
另一个解决办法是:

result['LTD_' + str(j)].iloc[[i]] = ...

什么是错误?如果使用安装的最后一行
result.iloc[[i]['LTD_'+str(j)]=0
?如果使用
打印(dt.date(result.iloc[[ltdloc]]].index.year,result.iloc[[ltdloc]]].index.month,result.iloc[[ltdloc]].index.day))
则打印失败?
结果.iloc[[i]['LTD_uu]+str(j)]=0
,则未发生任何事情。我还有今天的日期(
2017-05-25
)。但是,
print(dt.date(result.iloc[[ltdloc]]].index.year,result.iloc[[ltdloc]].index.month,result.iloc[[ltdloc]].index.day)]
'%Y-%m-%d'
的格式给出了一个好的日期,我想在
result.iloc[[I]['LTD]+str j]
.hmm,没有数据确实很难,但是
result['LTD'+str
结果['LTD_u'+str(j)]。iloc[[i]]
应该有效。它有效!非常感谢你。你是怎么知道的?或者更好的
result.loc[result.index[i],'LTD_'+str(j)]
result['LTD_'+str(j)]。iloc[[i]]
解决了我的问题。非常感谢你,耶斯雷尔。我把它添加到回答中;)如果我的回答有帮助,别忘了。谢谢