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

Get dataframe cell value based on substring

I am trying to search for a substring across my dataframe (no specific column) and get in return the value of the cell.

For example:

Column A Column B
Red apple carrots
Blue Car Banana

I’d like to search the cell which contains “Apple” and i should get in return “Red apple”.

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

i have been successful returning the row which contains the value as following:

df[df.apply(lambda row: row.astype(str).str.contains(“Apple”, case=false).any, axis=1)]

Or the column name which contains it:

df.apply(lambda x: x.str. contains(“Apple”, case=false).any())

But not exactly what i want, the full value of the cell which contains the substring.

Can I please get help?:)

>Solution :

One option is to stack and use boolean indexing:

s = df.stack()

out = s[s.str.contains('apple')]

output:

0  Column A    Red apple
dtype: object

If you expect a single match:

out = s[s.str.contains('apple')].squeeze()

output: 'Red apple'

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