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