Home > Net >  check if the element of a row is in another row
check if the element of a row is in another row

Time:01-27

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.

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

CodePudding user response:

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
  •  Tags:  
  • Related