DEV Community

GharamElhendy
GharamElhendy

Posted on

Fixing Up Column Names with Pandas

This serves to remove a common word from the names of multiple variables (For example: time_mean, area_mean, etc...)

remove "_mean" from column names

new_labels = []
for col in df.columns:
if '_mean' in col:
new_labels.append(col[:-5]) # exclude last 6 characters
else:
new_labels.append(col)

new_labels


Assigning the new column names to your original data set

df.columns = new_labels


Display the first few rows of dataframe to make sure that the changes applied

df.head()


Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay