3
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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.