Is there any way to change the index of the dataframe for ploting?

Viewed 37

I am trying to plot my project's data. The plot data is represented as the Index [0,1,2,3,4]. But I want to plot the data as index [3,0,1,4,5].

xx = df_new.groupby('Data' ,as_index=False).count().index

values = df_new.groupby('Data').size().values

plt.figure(figsize=(10,5))

plt.bar(xx ,values)

here is my data plot graph:

enter image description here

Is there any way to do this? Thank you.

1 Answers

Maybe try reindex. Say I have the following df

     A
0   10.0
1   NaN
2   20.0
3   30.0
4   40.0
5   50.0
6   60.0
7   NaN

I can plot

df.fillna(0).plot(kind='bar')

enter image description here

I can also rearrange as follows

df.reindex([7,6,4,2,1,3,0,5]).fillna(0).plot(kind='bar')

enter image description here

Related