Cheng
Cheng

Reputation: 206

Is there anyway to find the value in a column that has multiple values

I am trying to find a way to find the value in a column that has multiple value and return the name and ID from the dataframe.

Example DF

Name ID     N1       N2     N3     N4
John 354    1,2,4,5  4,5,6  7,8,9  1,2,4,5
Vane 444    4,3,     4,2,4  5,4,5  4,5,6
Lisa 654    1,3      5,6    5,6    2
Glen 454    1,3      6      7      8

For example I want to find if column N1:N4 have the value '2' and it will return the Name and ID

John , 354
Vane , 444
Lisa , 654

Upvotes: 0

Views: 58

Answers (1)

BENY
BENY

Reputation: 323226

IIUc using str.contains with any

df.loc[df.loc[:,'N1':].apply(lambda x : x.str.contains('2')).any(1),['Name','ID']]
   Name   ID
0  John  354
1  Vane  444
2  Lisa  654

Upvotes: 3

Related Questions