Home > Back-end >  run df.apply with lambda function in a loop
run df.apply with lambda function in a loop

Time:10-06

This code snippet works well:

df['art_kennz'] = df.apply(lambda x:myFunction(x.art_kennz), axis=1)

However, here I have hard coded the column name art_kennz on both places: df['art_kennz'] and x.art_kennz. Now, I want to modify the script such that I have a list of column names and the df.apply runs for all those columns. So I tried this:

cols_with_spaces = ['art_kennz', 'fk_wg_sch']
for col_name in cols_with_spaces:
    df[col_name] = df.apply(lambda x: myFunction(x.col_name)
                                               , axis=1)

but this gives an error that:

AttributeError: 'Series' object has no attribute 'col_name'

because of x.col_name. Here, col_name is supposed to be the element from the for loop. What would be the correct syntax for this?

CodePudding user response:

Try:

for col_name in cols_with_spaces:
    df[col_name] = df.apply(lambda x: myFunction(x[col_name])

Explanation: You can access the Serie using attribute syntax e.g x.art_kennz, but since col_name is a variable containing a string that represent the attribute, bracket syntax is the correct way.

CodePudding user response:

In this case x.art_kennz you use string but in for-loop you have variables you can not use .variables.

try this: (In this approach you iterate row by row)

for col_name in cols_with_spaces:
    df[col_name] = df.apply(lambda row: myFunction(row[col_name]), axis=1)

If you want to iterate columns by columns you can try this:

for col_name in cols_with_spaces:
    df[col_name] = df[col_name].apply(myFunction)
  • Related