To rename the columns of a pandas DataFrame in Python, you can use the rename()
method. Here is an example:
main.py296 chars13 lines
This will output the DataFrame with the new column names:
main.py128 chars5 lines
In this example, we define a dictionary new_columns
where the keys are the original column names and the values are the desired new column names. The rename()
method is then used to update the column names of the DataFrame.
Note that the rename()
method returns a new DataFrame with the updated column names, so we assign it back to df
to keep the changes.
Make sure to import the pandas
library before using it.
gistlibby LogSnag