How to Convert Index of a Pandas dataframe into a Column

Spread the love

Problem –

You have a pandas dataframe and you want to convert the index of the dataframe into a column.

Solution –

Reading a dataset for illustration.

import pandas as pd

url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/ICICIBANK.NS.csv'
df = pd.read_csv(url, index_col='Date')
df.head()

As you can see that we have an index which contains the Date of the trading. Now suppose you want to convert this Date index to a Date column. To do that, you can use the reset_index method in pandas.

df.reset_index()

If you permanently change the dataframe then pass inplace=True in the reset_index.

df.reset_index(inplace=True)

Rating: 1 out of 5.

Leave a Reply