Save value from dataframe after comparing it with a list value - Python

207 Views Asked by At

I am trying to compare the values from a list with the values of a dataframe column. In case both are equal, I want to save the whole row of the dataframe. I am not being able of writing the instruction to save the whole row of the dataframe.

Here are some data of the dataframe and list:

print(approval_polls.head(5))

 start_date    end_date         pollster    sponsor  sample_size population  \
0  2020-02-02  2020-02-04           YouGov  Economist       1500.0          a   
1  2020-02-02  2020-02-04           YouGov  Economist        376.0          a   
2  2020-02-02  2020-02-04           YouGov  Economist        523.0          a   
3  2020-02-02  2020-02-04           YouGov  Economist        599.0          a   
4  2020-02-07  2020-02-09  Morning Consult        NaN       2200.0          a   


excel_doc = ['Monmouth University' 'Selzer & Co.' 'ABC News/The Washington Post'
 'Siena College/The New York Times Upshot' 'YouGov']

The code I started writing is as follows:

approval_polls = approval_polls[approval_polls['pollster'].isin(excel_doc)]

The result I'm getting isn't right.

print (approval_polls)

[start_date, end_date, pollster, sponsor, sample_size, population, ...]

What is wrong in here?

Thank you for your suggestions

1

There are 1 best solutions below

2
On

Yes, isin is the way to go

excel_doc = ['Monmouth University' 'Selzer & Co.' 'ABC News/The Washington Post'
             'Siena College/The New York Times Upshot', 'YouGov']

df = df[df['pollster'].isin(excel_doc)]