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

check if the element of a row is in another row

I have a dataframe with 2 column, 1 containing number and the other containing list, I want to check if the column with number is in the column with the list

Testing data:

preds = [[40, 50, 21], [40, 50, 25], [40, 50, 21]]
target = [40, 50, 40]
df_testing = pd.DataFrame(list(zip(preds, target)),
           columns =['preds', 'target'])

so for example, in the first row, I want to check if 40 is in [40, 50, 21], for 2nd row, I want to check if 50 is in [40, 50, 25] etc.

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

desired result: Return a Serie of True False with the index of the row

>Solution :

Use in stamement:

preds = [[44, 55, 21], [40, 50, 25], [40, 50, 21]]
target = [40, 50, 40]
df_testing = pd.DataFrame(list(zip(preds, target)),
               columns =['preds', 'target'])

s = df_testing.apply(lambda x: x['target'] in x['preds'], axis=1)
print (s)
0    False
1     True
2     True
dtype: bool
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