将数据框值从 int 类型更改为 date
Chanding a dataframe values from int type to date
我有以下数据框
df = pd.DataFrame({
'date': [1988, 1988, 2000, 2005],
'value': [2100, 4568, 7896, 68909]
})
我想根据这个df做一个时间序列。我如何将年份从 int 更改为 datetimeindex,以便绘制时间序列?
df = pd.DataFrame({
'date': [1988, 1988, 2000, 2005],
'value': [2100, 4568, 7896, 68909]
})
date = []
for year in df.date:
date.append(pd.datetime(year,1,1))
df.index=date
df['value'].plot()
使用:pd.to_datetime
to convert to datetime. DataFrame.set_index
in order to get and plot the Series
. You can plot it with Series.plot
(df.assign(date = pd.to_datetime(df['date'],format = '%Y'))
.set_index('date')['value']
.plot())
如果你想保留这个系列,请使用:
s = (df.assign(date = pd.to_datetime(df['date'],format = '%Y'))
.set_index('date')['value'])
然后:
s.plot()
我有以下数据框
df = pd.DataFrame({
'date': [1988, 1988, 2000, 2005],
'value': [2100, 4568, 7896, 68909]
})
我想根据这个df做一个时间序列。我如何将年份从 int 更改为 datetimeindex,以便绘制时间序列?
df = pd.DataFrame({
'date': [1988, 1988, 2000, 2005],
'value': [2100, 4568, 7896, 68909]
})
date = []
for year in df.date:
date.append(pd.datetime(year,1,1))
df.index=date
df['value'].plot()
使用:pd.to_datetime
to convert to datetime. DataFrame.set_index
in order to get and plot the Series
. You can plot it with Series.plot
(df.assign(date = pd.to_datetime(df['date'],format = '%Y'))
.set_index('date')['value']
.plot())
如果你想保留这个系列,请使用:
s = (df.assign(date = pd.to_datetime(df['date'],format = '%Y'))
.set_index('date')['value'])
然后:
s.plot()