I'm trying to add some dummy variable columns to a list of databases in a for loop, however while the code works outside the loop as expected, it doesn't work when inside the loop. Here's a minimal working example- note when printing the df inside the loop it has the expected form, but then calling df1 and df2 outside the loop they don't have the additional columns.
import pandas as pd
df1=pd.DataFrame({'A':['a','b','c']})
df2=pd.DataFrame({'A':['b','c','b']})
combine=[df1,df2]
for df in combine:
df=pd.concat([df,pd.get_dummies(df['A'])],axis=1)
print(df)
print(df1)
df1=pd.concat([df1,pd.get_dummies(df1['A'])],axis=1)
print(df1)
A a b c
0 a 1 0 0
1 b 0 1 0
2 c 0 0 1
A b c
0 b 1 0
1 c 0 1
2 b 1 0
A
0 a
1 b
2 c
A a b c
0 a 1 0 0
1 b 0 1 0
2 c 0 0 1
Thanks for all your help.