Search code examples
pythonpandasdataframedata-analysisattributeerror

run df.apply with lambda function in a loop


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?


Solution

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