如果我有这样的DataFrame
Date value
04 May 2015 1
06 May 2015 1
07 May 2015 1
11 May 2015 1
11 May 2015 1
如何获得日期索引的差异?即下面的第三个栏目:
Date value Diff
04 May 2015 1 NA
06 May 2015 1 2
07 May 2015 1 1
11 May 2015 1 4
11 May 2015 1 0
答案 0 :(得分:2)
你的意思是:
df["Diff"] = df.index
df["Diff"] = (df['Diff'] - df['Diff'].shift())
print(df)
value Diff
Date
2015-05-04 1 NaT
2015-05-06 1 2 days
2015-05-07 1 1 days
2015-05-11 1 4 days
2015-05-11 1 0 days
答案 1 :(得分:0)
您可以使用pandas.Series.diff
>>> df['diff_in_days'] = df.index.to_series().diff()
value Diff
Date
2015-05-04 1 NaT
2015-05-06 1 2 days
2015-05-07 1 1 days
2015-05-11 1 4 days
2015-05-11 1 0 days