简体   繁体   中英

Rename Columns in a Pandas Dataframe with values form dictionary

I have a pandas data frame read from an excel file. Note : the column names remain the same but the position of the column might vary in the excel file.

df

    colA    colB    colC   ...
0   val11   val12   val13  ... 
1   val21   val22   val23  ...
... ... ...

I have a list of dictionaries that should be used to change the column names, which is as below

field_map

    [{"file_field" : "colA" , "table_field" : "tab1"},
     {"file_field" : "colB" , "table_field" : "tab2"},
     {"file_field" : "colC" , "table_field" : "tab3"},
      ... ... ...]

I could convert the column keys for each row in the DataFrame separately in this way and using the new_dt for further operations.

file_dt = df.to_dict("records")

for each_entry in file_dt:
    new_dt = {}
    for field in field_map:      
        new_dt[field['table_field'] = each_entry[field['file_field']]
    ... ... ...

This method is taking too long when my file is large.

I want to change the column headers of the data Frame before processing the entries further, this will reduce a lot of processing time for me. Kindly help me with this. I'm expecting the data frame to be something like this

Expected df

    tab1    tab2    tab3   ...
0   val11   val12   val13  ... 
1   val21   val22   val23  ...
... ... ...

Thanks in Advance

Just use the rename function in your existing dataframe df :

df = df.rename(columns={"colA":"tab1", "colB":"tab2", "colB":"tab3"})

You would need to modify the field_map dictionary a bit:

col_rename_dict = {el["file_field"]:el["table_field"] for el in field_map}
df = df.rename(columns=col_rename_dict)

If you change your field_map you can simply pass it to df.rename as:

field_map = {x['file_field']:x['table_field'] for x in field_map}
df = df.rename(columns=field_map)

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM