Home > front end >  Python : How to use lambda function to return a specific value?
Python : How to use lambda function to return a specific value?

Time:01-15

I have a dataframe with a column "A" whose values can be as such:

[';', '', 'M;', 'M', ';M', 'M;M', ';;M']

I would like to return the value 'M' in my second column "B" if I can find "M" (it doesn't matter if there are several M's) in the row on column A. If there is no M, the row in B should stay empty. I'm having trouble translating this into a lambda function. Can anyone help please?

df['B']=df['A'].apply(lambda x:x[....... if 'M' in x else None])

CodePudding user response:

Just use the expression you want to return before the if keyword:

df['B']=df['A'].apply(lambda x:'M' if 'M' in x else None)

The keys to understand this is that "lambda" will resolve a single expression, and return its value, and the inline " ... if ... else ..." construct in Python just evaluates to the first or the last part depending on the test expression.

If you were not using apply in a single column, but on the whole dataframe (if you'd need to check values in more than one column, for example), you have to pass the "axis=1" parameter to apply. In that case, though not needed, it would be nice to have an extra pair of parentheses around the lambda. Otherwise the burden falls on the readers of your code to determine if the "," separating the lambda body from the next argument is really a separator or part of the lambda:

df['B']=df.apply((lambda row:'M' if 'M' in row['A'] else None), axis=1)

CodePudding user response:

You do not need to use a lambda function. Avoid the loop and use loc with str.contains

df = pd.DataFrame({'A': [';', '', 'M;', 'M', ';M', 'M;M', ';;M']})
df.loc[df['A'].str.contains('M'), 'B'] = 'M'

     A    B
0    ;  NaN
1       NaN
2   M;    M
3    M    M
4   ;M    M
5  M;M    M
6  ;;M    M

CodePudding user response:

add parameter axis, or the function will be applied to every column. And that will make the code like:

df["B"] = df.apply(lambda x: 'M' if ('M' in x['A']) else None ,axis=1)
  •  Tags:  
  • Related