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)