To rename a column in a pandas DataFrame in Python, you can use the rename()
method. Here's the syntax:
main.py72 chars2 lines
In this syntax, df
is the DataFrame you want to modify, old_column_name
is the name of the column you want to rename, and new_column_name
is the new name you want to assign to the column.
The inplace=True
parameter ensures that the modification is made directly to the DataFrame, without creating a new copy.
Here's an example:
main.py236 chars12 lines
Output:
main.py60 chars5 lines
In this example, the column 'Age' is renamed to 'Years' using the rename()
method.
Remember to assign the modified DataFrame back to the original variable or a new variable if you intend to use it further.
gistlibby LogSnag