The pandas.rename()
function is used to rename columns or rows in a pandas DataFrame. Here's an example of how to use it in Python:
main.py434 chars19 lines
In the above example, we have a DataFrame with columns 'Name', 'Age', and 'City'. We want to rename the 'Name' column to 'First Name' and the 'City' column to 'City Name'. The pandas.rename()
function takes a dictionary as an argument, where the keys are the old column names and the values are the new column names. The function updates the DataFrame with the new column names.
Output:
main.py259 chars12 lines
Note that pandas.rename()
returns a new DataFrame with the updated column names, so we assign the returned DataFrame back to df
.
gistlibby LogSnag