df['expens'] = df.apply(lambda z : z['total_bill'] * 2 if z['total_bill'] < 10 ,axis = 1)
I want to apply the lambda expression using the if condition and create new column variable df['expens']
under the condition if df['total_bill] is < 10
I tried the above the python syntax but is throws an error "invalid syntax"
Apply the condition during indexing, not in the lambda if you only want to apply the function to those rows for which the condition is true. Also no need for a lambda if just multiplying in pandas.
df.loc[df["total_bill"]<10, "expens"] = df["total_bill"] * 2
NB. it is possible to have a condition in a lambda with the ternary operator lambda x: x * 2 if x < 10 else x
but not useful in this case.