Member-only story
Renaming pandas DataFrame columns — with examples.
In the blog post, How to drop columns from a Pandas DataFrame — with examples., I covered examples of how to completely remove certain columns from a pandas DataFrame. But what if you need to keep the columns, yet their names are not to your liking? Perhaps you need to provide a report with meaningful column names in a CSV or Excel file? Again, pandas make this relatively simple. Let’s understand with some examples…
OS, Database, and software used:
- OpenSuse Leap 15.0
- Python 3.7.2
Self-Promotion:
If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!
Like always, we need to get pandas imported. Then, for this specific example, I’ll create a DataFrame object of CSV data with the function:
>>> import pandas as pd
>>> df = pd.read_csv(‘/home/joshua/Practice Data/Fitness_DB_Data/aug_stats.csv’, delimiter=’,’)
With the function, I’ll retrieve the first 5 rows (the default) from the DataFrame so we can get a sense of its structure and data:
>>> df.head()
day_walked cal_burned miles_walked…