Olaola
Olaola

Reputation: 29

Incorrect data output with pandas

I have a csv file that looks as follows:

start_date,end_date,pollster,sponsor,sample_size,population,party,subject,tracking,text,approve,disapprove,url
    2020-02-02,2020-02-04,YouGov,Economist,1500,a,all,Trump,FALSE,Do you approve or disapprove of Donald Trump’s handling of the coronavirus outbreak?,42,29,https://d25d2506sfb94s.cloudfront.net/cumulus_uploads/document/73jqd6u5mv/econTabReport.pdf
    2020-02-02,2020-02-04,YouGov,Economist,376,a,R,Trump,FALSE,Do you approve or disapprove of Donald Trump’s handling of the coronavirus outbreak?,75,6,https://d25d2506sfb94s.cloudfront.net/cumulus_uploads/document/73jqd6u5mv/econTabReport.pdf
    2020-02-02,2020-02-04,YouGov,Economist,523,a,D,Trump,TRUE,Do you approve or disapprove of Donald Trump’s handling of the coronavirus outbreak?,21,51,https://d25d2506sfb94s.cloudfront.net/cumulus_uploads/document/73jqd6u5mv/econTabReport.pdf
    2020-02-02,2020-02-04,YouGov,Economist,599,a,I,Trump,,Do you approve or disapprove of Donald Trump’s handling of the coronavirus outbreak?,39,25,https://d25d2506sfb94s.cloudfront.net/cumulus_uploads/document/73jqd6u5mv/econTabReport.pdf
    2020-02-07,2020-02-09,Morning Consult,"",2200,a,all,Trump,TURE,Do you approve or disapprove of the job each of the following is doing in handling the spread of coronavirus in the United States? President Donald Trump,57,22,https://morningconsult.com/wp-content/uploads/2020/02/200214_crosstabs_CORONAVIRUS_Adults_v4_JB.pdf

I am interested in the column "tracking" that has values "TURE", "FALSE" or NAN

For some reason, when I read it with pandas, the all of the "tracking" column values are loaded as "False":

data = pd.read_csv("covid_approval_polls.csv")
data.head() 

start_date  end_date    pollster    sponsor     sample_size     population  party   subject     tracking    text    approve     disapprove  url
0   2020-02-02  2020-02-04  YouGov  Economist   1500.0  a   all     Trump   False   Do you approve or disapprove of Donald Trump’s...   42.0    29.0    https://d25d2506sfb94s.cloudfront.net/cumulus_...
1   2020-02-02  2020-02-04  YouGov  Economist   376.0   a   R   Trump   False   Do you approve or disapprove of Donald Trump’s...   75.0    6.0     https://d25d2506sfb94s.cloudfront.net/cumulus_...
2   2020-02-02  2020-02-04  YouGov  Economist   523.0   a   D   Trump   False   Do you approve or disapprove of Donald Trump’s...   21.0    51.0    https://d25d2506sfb94s.cloudfront.net/cumulus_...
3   2020-02-02  2020-02-04  YouGov  Economist   599.0   a   I   Trump   False   Do you approve or disapprove of Donald Trump’s...   39.0    25.0    https://d25d2506sfb94s.cloudfront.net/cumulus_...
4   2020-02-07  2020-02-09  Morning Consult     NaN     2200.0  a   all     Trump   False   Do you approve or disapprove of the job each o...   57.0    22.0    https://morningconsult.com/wp-content/uploads/.

..

When I search for the unique values of that column with the command:

data.tracking.unique()

I get the correct output:

array([False, True, nan], dtype=object)

But when I execute the command:

print(data[data["tracking"] == "FALSE"])

I get:

Empty DataFrame
Columns: [start_date, end_date, pollster, sponsor, sample_size, population, party, subject, tracking, text, approve, disapprove, url]
Index: []

I am quite sure I am missing something here, but have no idea what might be causing the problem? I would like to get the rows based on the column "tracking" value "FALSE"

Upvotes: 0

Views: 292

Answers (1)

Corralien
Corralien

Reputation: 120429

To force type, use dtype parameter:

data = pd.read_csv("covid_approval_polls.csv", dtype={"tracking": str})

Upvotes: 2

Related Questions