How to change index of dataframe?

I have a DataFrame with an index column as [0, 1,2, … 11]. I would like to change it to [β€˜Jan’, β€˜Feb’, β€¦β€˜Dec’]. how do I do this?

Hi @vivek22 ,

df = pd.DataFrame(data=data)

# change the columns of dataframe
df.columns = ["Jan", "Feb", …"Dec"]

Hope this help.

I tried that but the index does not change.

Hi @vivek22 ,

Can you share the code ?

It will be a big help reproducing your issue.

I think confusion here may be you are thinking [0, 1, …] are columns. They are not, they are the row elements of Index column that need to be changed to [β€˜Jan’, β€˜Feb’ …]

Hi @vivek22 ,

Oh so sorry, is it index column right ?

# add your name of months as a new column to your dataframe, for example a column named`month`
df["month"] = ["Jan", "Feb" …]

# Set the index column to `month` column
df = df.set_index(['month'])

Hope this help.

1 Like

Thanks a lot! And apologize for the late response due to other commitments.

Hi @vivek22 ,

I am happy to help. :slight_smile:

2 Likes