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])
>Solution :
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)