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

Extract rows Pandas If the value of column is contained in another column

I have the following dataset (see figure)
dataframe

I need to extract all rows from the Results data frame such that Results[‘Class’] is in Results[Path_x’]

(In the specific case, it should only return me the second-to-last line because Results[‘Class’] is totally contained in Results[‘Path_x’] in the penultimate case)

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 tried the following line of code:

results=results[results['Class'].isin(results['Path_x'])]

However, that line always generates an empty data frame.

>Solution :

You can do

mask = results.apply(lambda x: ['Class'] in results['Path_x'], axis=1)
results= results.loc[mask, :]

See this question: Python Pandas: Check if string in one column is contained in string of another column in the same row

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