Reputation: 315
I'm trying to harmonize the column names in all my data frames so that I can concatenate them and create one table. I'm struggling to create a loop over multiple dataframes. The code does not fail, but it does not work either. Here is an example of two dataframes and a list that includes the dataframes:
df_test = pd.DataFrame({'HHLD_ID':[6,7,8,9,10],
'sales':[25,50,25,25,50],
'units':[1,2,1,1,2],
})
df_test2 = pd.DataFrame({'HHLD_ID':[1,2,3,4,5],
'sale':[25,50,25,25,50],
'unit':[1,2,1,1,2],
})
list_df_export = [df_test,df_test2]
Here is what I have tried...
for d in list_df_export:
if 'sale' in d:
d = d.rename(columns={"sale": "sales",'unit':'units'})
Here is what I would like df_test2 to look like...
Upvotes: 1
Views: 74
Reputation: 26
Maybe the "inplace" option can help you:
for d in list_df_export:
if 'sale' in d:
d = d.rename(columns={"sale": "sales", 'unit': 'units'}, inplace=True)
Upvotes: 1
Reputation: 75080
you can use:
d = {'sale':'sales','unit':'units'}
pd.concat(i.rename(columns=d) for i in list_df_export)
Upvotes: 2
Reputation: 2239
You can try:
df_test2.columns = df_test.columns
This will make the columns in df_test2 have the same names as df_test.
Is this what you need?
Upvotes: 0