🎉 AI Engineers: Aporia's 2024 Benchmark Report and mutiSLM has been released. View the report here>>

Aporia How to's

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

2 min read

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

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:

Green Background

Control All your GenAI Apps in minutes