How do you rename a dataframe in python?
You can use one of the following three methods to rename columns in a pandas DataFrame: Show Method 1: Rename Specific Columns df.rename(columns = {'old_col1':'new_col1', 'old_col2':'new_col2'}, inplace = True) Method 2: Rename All Columns df.columns = ['new_col1', 'new_col2', 'new_col3', 'new_col4'] Method 3: Replace Specific Characters in Columns df.columns = df.columns.str.replace('old_char', 'new_char') The following examples show how to use each of these methods in practice. Method 1: Rename Specific ColumnsThe following code shows how to rename specific columns in a pandas DataFrame: import pandas as pd #define DataFrame df = pd.DataFrame({'team':['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], 'points': [25, 12, 15, 14, 19, 23, 25, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #list column names list(df) ['team', 'points', 'assists', 'rebounds'] #rename specific column names df.rename(columns = {'team':'team_name', 'points':'points_scored'}, inplace = True) #view updated list of column names list(df) ['team_name', 'points_scored', 'assists', 'rebounds'] Notice that the ‘team’ and ‘points’ columns were renamed while all other column names remained the same. Method 2: Rename All ColumnsThe following code shows how to rename all columns in a pandas DataFrame: import pandas as pd #define DataFrame df = pd.DataFrame({'team':['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], 'points': [25, 12, 15, 14, 19, 23, 25, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #list column names list(df) ['team', 'points', 'assists', 'rebounds'] #rename all column names df.columns = ['_team', '_points', '_assists', '_rebounds'] #view updated list of column names list(df) ['_team', '_points', '_assists', '_rebounds'] Note that it’s faster to use this method when you want to rename most or all of the column names in the DataFrame. Method 3: Replace Specific Characters in ColumnsThe following code shows how to replace a specific character in each column name: import pandas as pd #define DataFrame df = pd.DataFrame({'$team':['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], '$points': [25, 12, 15, 14, 19, 23, 25, 29], '$assists': [5, 7, 7, 9, 12, 9, 9, 4], '$rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #list column names list(df) ['team', 'points', 'assists', 'rebounds'] #rename $ with blank in every column name df.columns = df.columns.str.replace('$', '') #view updated list of column names list(df) ['team', 'points', 'assists', 'rebounds'] Notice that this method allowed us to quickly remove the ‘$’ from each column name. Additional ResourcesThe following tutorials explain how to perform other common operations in pandas: How to List All Column Names in Pandas It's quite simple to rename a DataFrame column name in Pandas. All that you need to do is to use the rename() method and pass the column name that you want to change and the new column name. Let's take an example and see how it's done. Steps
Exampleimport pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] } ) print "Input DataFrame is:\n", df df = df.rename(columns={"x": "new_x"}) print "After renaming, the DataFrame is:\n", df OutputInput DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 After renaming, the DataFrame is: new_x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1
Updated on 14-Sep-2021 13:40:06
How do I rename in Pandas?One way of renaming the columns in a Pandas Dataframe is by using the rename() function. This method is quite useful when we need to rename some selected columns because we need to specify information only for the columns which are to be renamed.
What is the use of rename function in DataFrame?DataFrame - rename() function
The rename() function is used to alter axes labels. Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don't throw an error.
How do you rename a value in a data frame?To rename index values of pandas DataFrame use rename() method or index attribute.
How do I change the header name in a DataFrame?Way 1: Using rename() method. Import pandas.. Create a data frame with multiple columns.. Create a dictionary and set key = old name, value= new name of columns header.. Assign the dictionary in columns.. Call the rename method and pass columns that contain dictionary and inplace=true as an argument.. |