Search code examples
pythonpandaslambda

Using Two Variables In Lambda Python


I want to make a new column based on two variables. I want my new column to have the value "Good" if (column 1 >= .5 or column 2 < 0.5) and (column 1 < .5 or column 2 >= 0.5) otherwise "Bad".

I tried using lambda and if.

df["new column"] = df[["column 1", "column 2"]].apply(
    lambda x, y: "Good" if (x >= 0.5 or y < 0.5) and (x < 0.5 or y >= 0.5) else "Bad"
)

Got

TypeError: ("() missing 1 required positional argument: 'y'", 'occurred at index column 1')

Solution

  • Try this:

    import pandas as pd 
    
    def update_column(row):
        if (row['x'] >= .5 or row['y'] <= .5) and (row['x'] < .5 or row['y'] >= .5):
            return "Good"
        return "Bad"
    
    df['new_column'] = df.apply(update_column, axis=1)