
# reset index with custom name for the new original index column Let’s give the resulting column the name “Employee”. Note that the names parameter was introduced in the pandas version 1.5.0, so check your pandas version if this method is not working for you. The idea is to pass the name of the column with the original index data as an argument to the names parameter in the reset_index() function. Using the names parameter in the reset_index() function Let’s look at the three methods with examples.
After applying the reset_index() function, use the pandas dataframe rename() function to change the column names to your liking.
Before applying the reset_index() function, give the index axis a name (the name you’d like the column to have), and then apply the reset_index() method.
Note that this method works only for pandas version 1.5.0 and above (as the names parameter was introduced to this function in the version 1.5.0).
In the call to the reset_index() function, specify the name you’d like the column with the original index data to have using the names parameter. Now, if you’d like the new column to have a different name, you can use one of the following three methods – This is because the original index in the dataframe didn’t have a name for itself. Notice, that the column with the previous index has the name “index”. You’d get the previous index (we say previous index because the reset_index() function “resets” the index to the default integer index starting from 0) as a separate column. Now, if you were to apply the reset_index() function with default parameters. You can see that the index in this particular dataframe is the name of the respective employee. Here, we created a dataframe with some information about some employees in an office. "Department": ,ĭf = pd.DataFrame(data, index=) Let’s now look at both these methods with the help of some examples.įirst, we will create a dataframe that we’ll use throughout this tutorial. After applying the reset_index() function, apply the pandas dataframe rename() function on the resulting dataframe to change one or more column names as per your requirements. Before using the reset_index() function, give the index axis the name you’d like the resulting column to have using the rename_axis() function and then apply the reset_index() function. Pass a list of names if the dataframe has a multi-index. For pandas version 1.5.0 and above, when calling the reset_index() function, pass the name you’d like the column (with the previous index) to have with the help of the names parameter.
You can, however, change the name of this column in the following ways – If the index axis itself doesn’t have a name, the new column is added with the name “index”. Methods to rename the column resulting from reset_index() The pandas dataframe reset_index() function is used to reset the index of a dataframe and by default, it adds the previous index as a separate column in the dataframe (pass drop=True if you do not want to retain the previous index as a separate column).
Pandas rename how to#
In this tutorial, we will look at how to rename the column resulting from the reset_index() function in pandas with the help of some examples.