Home > other >  Iterating through multiple data frames with for loop to change column names to lower case and snake
Iterating through multiple data frames with for loop to change column names to lower case and snake

Time:06-10

I am trying to utilize a for-loop to iterate through multiple data frames and change each df's column names to snake case and lower case. I made a data frame list and then created a nested for-loop to iterate through the list and then iterate through each column name. The loop runs but it makes no changes to my data frame. Any help on this?

    df_list = [df_1, df_2, df_3, df_4, df_5]

    for df in df_list:
        for col in df.columns:
            col.replace(' ', '_').lower()

CodePudding user response:

You didn't assign the replaced column back, you can try

for df in df_list:
    df.rename(columns=lambda col: col.replace(' ', '_').lower(), inplace=True)
  • Related