Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change Column Names in Dataframe via list of Column Names

I have a dataframe with a ton of columns. I would like to change a list of a sub set of the column names to all uppercase.

The code below doesn’t change the column names and the other code I’ve tried produces errors:

df[cols_to_cap].columns = df[cols_to_cap].columns.str.upper()

What am I missing?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try the below code, this uses the rename function.

rename_dict = {}    
for each_column in list_of_cols_in_lower_case:
    rename_dict[each_column] = each_column.upper()
df.rename(columns = rename_dict , inplace = True ) #inplace to True if you want the change to be applied to the dataframe
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading