Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python : How to use lambda function to return a specific value?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading