Rename Columns of pandas DataFrame in Python (2 Examples) | Change Variable Names | rename Function

Published: 24 February 2023
on channel: Statistics Globe
220
10

How to modify the names of columns in a pandas DataFrame in the Python programming language. More details: https://statisticsglobe.com/rename-co...
Python code of this video:

import pandas as pd # Load pandas

data = pd.DataFrame({"x1":range(7, 1, - 1), # Create pandas DataFrame
"x2":["a", "b", "c", "d", "e", "f"],
"x3":["X", "Y", "X", "X", "Y", "X"]})
print(data) # Print pandas DataFrame

data_new1 = data.copy() # Create copy of DataFrame
data_new1.columns = ["col1", "col2", "col3"] # Using columns attribute
print(data_new1) # Print updated pandas DataFrame

data_new2 = data.copy() # Create copy of DataFrame
data_new2 = data_new2.rename(columns = {"x1": "col1", "x3": "col3"}) # Using rename function
print(data_new2) # Print updated pandas DataFrame

Follow me on Social Media:
Facebook – Statistics Globe Page:   / statisticsglobecom  
Facebook – R Programming Group for Discussions & Questions:   / statisticsglobe  
Facebook – Python Programming Group for Discussions & Questions:   / statisticsglobepython  
LinkedIn – Statistics Globe Page:   / statisticsglobe  
LinkedIn – R Programming Group for Discussions & Questions:   / 12555223  
LinkedIn – Python Programming Group for Discussions & Questions:   / 12673534  
Twitter:   / joachimschork  

Music by bensound.com