To rename dataframe columns in Python using pandas, you can use the rename
method. This method allows you to specify new column names for one or more columns.
Here is an example of how to rename dataframe columns:
main.py295 chars14 lines
Output:
main.py56 chars5 lines
In the code above, we first create a dataframe with columns 'A' and 'B'. Then, we use the rename
method to rename the columns. The columns
parameter of the rename
method takes a dictionary where the keys are the original column names and the values are the new column names. By setting inplace=True
, the renaming is done directly on the dataframe.
Note that if you don't set inplace=True
, the rename
method will return a new dataframe with the renamed columns, but the original dataframe will remain unchanged.
gistlibby LogSnag