更改数据帧的索引索引号

问题描述 投票:0回答:1

我有以下数据框:

Month
1    -0.075844
2    -0.089111
3     0.042705
4     0.002147
5    -0.010528
6     0.109443
7     0.198334
8     0.209830
9     0.075139
10   -0.062405
11   -0.211774
12   -0.109167
1    -0.075844
2    -0.089111
3     0.042705
4     0.002147
5    -0.010528
6     0.109443
7     0.198334
8     0.209830
9     0.075139
10   -0.062405
11   -0.211774
12   -0.109167
Name: Passengers, dtype: float64

如您所见,数字在1-12 / 1-12中被列出两次,相反,我想将索引更改为1-24。问题是在绘制时会看到以下内容:

plt.figure(figsize=(15,5))
plt.plot(esta2,color='orange')
plt.show()

Plot

我想看到从1到24的连续线。

python pandas numpy dataframe matplotlib
1个回答
0
投票

esta2 = esta2.reset_index()将为您提供0-23。如果您需要1-24,则只需执行esta2.index = np.arange(1, len(esta2) + 1)


0
投票

或更简单地说:

df.index = [i for i in range(1,len(df.index)+1)]
df.index.name = 'Month'

print(df)
           Val
Month          
1     -0.075844
2     -0.089111
3      0.042705
4      0.002147
5     -0.010528
6      0.109443
7      0.198334
8      0.209830
9      0.075139
10    -0.062405
11    -0.211774
12    -0.109167
13    -0.075844
14    -0.089111
15     0.042705
16     0.002147
17    -0.010528
18     0.109443
19     0.198334
20     0.209830
21     0.075139
22    -0.062405
23    -0.211774
24    -0.109167
© www.soinside.com 2019 - 2024. All rights reserved.