Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping to modify multiple Dataframes in a list

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.

like image 488
Arihant Jain Avatar asked Feb 01 '26 19:02

Arihant Jain


1 Answers

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.

like image 56
Mustafa Aydın Avatar answered Feb 03 '26 08:02

Mustafa Aydın