0

I have a pandas dataframe like this

**First_name Last_name  Policy_number**
David        Gilbert    IOTA123455
Fredrick     Manuel     IOTA367522
Donald       Trump      IOTA337823
Cathy        Jackso     IOTA892352

And I have a variable like this in my python program:

some_value = "Trump"

The "some_value" can take any value from any column from the above pandas dataframe. My query has to find in which column that some_value is, and return the record.

Say for ex, if some_value = "Trump", my output should be:

 **First_name Last_name Policy_number**
    Donald       Trump      IOTA337823

Or if some_value = "IOTA123455", my output should be:

**First_name Last_name  Policy_number**
David        Gilbert    IOTA123455

To achieve this, i have used the below code:

df.loc[(df['First_Name'].isin(some_value)) | df['Last_Name'].isin(some_values)] | df['Policy_Number'].isin(some_values)]

However i got the error as:

TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]

Is there any other way to solve this problem? Please help out.

Doubt Dhanabalu
  • 457
  • 4
  • 8
  • 18
  • For starters, just put: `some_value = ['Trump']`, your code will work. – YOLO Mar 22 '18 at 17:34
  • You want `df.loc[(df==some_value).sum(axis=1).astype(bool)]`. This way you dont have to type out the same isin command for every single row. – ALollz Mar 22 '18 at 17:41

0 Answers0