for k in [df1,df2,df3,df4,df5]:
y = pd.get_dummies(k['Fuel_Type'], prefix='Fuel')
k = pd.concat([y,k], axis=1)
k.drop('Fuel_Type', axis=1, inplace=True)
I have 5 dataframes with column Fuel_Type which I want to be dummied and concatenated on the same dataframe. I have tried this out there is no error but the dataframes are not modified after this loop they are same as they were before.
My output should be like all 5 dataframes with dummy variable columns of Fuel_Type variable.
You can wrap your operations in a function and then return & assign the results back:
def add_dummies(df, col_name="Fuel_Type", prefix="Fuel"):
dummies = pd.get_dummies(df[col_name], prefix=prefix)
df = pd.concat([dummies, df], axis=1)
df = df.drop(col_name, axis=1)
return df
df_list = [df1, df2, df3, df4, df5]
df1, df2, df3, df4, df5 = [add_dummies(df) for df in df_list]
# or
# df1, df2, df3, df4, df5 = map(add_dummies, df_list)
The reason yours is not modifying dataframes is that you are reassigning k in pd.concat line and from then on, it "points" to another object which is different than df_j for each j.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With