How to Convert the Index of a DataFrame to a Column?

convert the index of a series into a column of a dataframe pandas

DataFrame is a two-dimensional data structure with labeled rows and columns. Row labels are also known as the index of a DataFrame.ย 

In this short how-to article, we will learn how to create a column from the index of Pandas DataFrames.

convert the index of a series into a column of a dataframe pandas


The reset_index function resets the index of the DataFrame with the default index which is integers starting from 0 and creates a column with the existing index of the DataFrame.

					df = df.reset_index()


The new column is named as โ€œindexโ€ by default but we can change its name using the rename function.

					df = df.reset_index().rename(columns={"index":"date"})


Letโ€™s say you just want to reset the index but do not want to use the existing one in the DataFrame. In this case, the value of the drop parameter needs to be changed to False.

					df = df.reset_index(drop=True)


Note: We cannot use the reset_index function on PySpark DataFrames because Spark distributes the data and has no concept of index.

This question is also being asked as:

  • How to reset index in a Pandas DataFrame

People have also asked for:

You may also like

Start Monitoring Your Models in Minutes