How to rename column name in pandas
The Pandas DataFrame rename() method can be used to rename one or more columns at once:
Pandas is a popular data manipulation library in Python, used for data analysis, cleaning, and manipulation tasks. One common task is to rename columns in a Pandas DataFrame. There are several methods to rename columns in Pandas, including the rename() method, the columns attribute, and the set_axis() method. Let's explore each method in detail:
Method 1: Using the rename() method
The rename() method is the most commonly used method to rename columns in a Pandas DataFrame. The rename() method can be used to rename one or more columns at once, by passing a dictionary of old column names to new column names as the argument.
In the above example, we used the rename() method to rename the 'Grade' column to 'Level'. The old column name 'Grade' is passed as a key, and the new column name 'Level' is passed as a value in a dictionary.
Method 2: Using the columns attribute
The columns attribute is another way to rename columns in a Pandas DataFrame. It allows you to directly assign a list of new column names to the columns attribute of the DataFrame.
In the above example, used the columns attribute to assign a list of new column names to the columns attribute of the DataFrame.
Method 3: Using the set_axis() method
The set_axis() method is a lesser-known method to rename columns in a Pandas DataFrame. It allows you to rename both rows and columns of a DataFrame at once.
Method 4: using str.replace() function
To rename multiple columns in Pandas using the DataFrame.columns.str.replace function, you can use regular expressions to match the old column names and replace them with the new column names.
In the above example used the DataFrame.columns.str.replace function to rename the 'Grade' column to 'Level' and 'Promoted' column to 'Passed'. Here, used two separate str.replace methods, one for each old column name we want to replace. The str.replace method updates the column names of the DataFrame in place and returns the updated DataFrame.
Rename a specific character in all column pandas
To rename a specific character in all columns of a Pandas DataFrame, you can use the str.replace() method of the DataFrame columns.
In the above example, used the str.replace() method to replace the 'School_' character in all column names with 'Class_'. So, accessed the columns of the DataFrame using the .columns attribute and then applied the str.replace() method to each column name. The updated column names are then assigned back to the columns attribute of the DataFrame.
Conclusion
To rename a column in a Pandas DataFrame, you can use the rename() method. You need to provide a dictionary with the current column name as the key and the new column name as the value. This method will update the DataFrame with the new column names, and the original DataFrame will be modified in place.
- Creating an empty Pandas DataFrame
- How to Check if a Pandas DataFrame is Empty
- How to check if a column exists in Pandas Dataframe
- How to delete column from pandas DataFrame
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- How to Drop rows in DataFrame by conditions on column values
- Get a List of all Column Names in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Change the order of columns in Pandas dataframe
- Concatenate two columns into a single column in pandas dataframe
- How to count the number of rows and columns in a Pandas DataFrame
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- Convert list of dictionaries to a pandas DataFrame
- How to set a particular cell value in pandas DataFrame