Skip to main content
 首页 » 编程设计

python之更改系列的单个索引

2024年10月24日13shanyou

我正在尝试更改系列的特定 索引。索引是 Date。 cashflow_series 是系列,我只是想通过使用 replace(month = cashflow_series.index[k].month + dict_no_months_between_payments[count]) 在索引上并使用它来替换以前的索引日期。我目前正在使用 reindex,但这会更改所有索引,因此我不知道如何只更改一个索引。

index_here = cashflow_series.index[k] 
cashflow_series.index[k] = cashflow_series.index[k].reindex(index=index_here.replace(month = cashflow_series.index[k].month + dict_no_months_between_payments[count])) 

谢谢

请您参考如下方法:

在 Pandas 中,索引是卡住的(它是不可变的,如果您尝试更改索引中的值,将会出现以下错误):

TypeError: '' does not support mutable operations.

您需要创建旧索引值的副本,修改它,然后用这个副本替换您系列中的那个。像这样的东西:

idx = df.index.values 
idx[3] = '2015-8-15' 
df.index = idx