Home > Mobile >  Why pandas cannot read correctly a csv file?
Why pandas cannot read correctly a csv file?

Time:04-29

I want to read data from a .csv file into a pandas dataframe. I have the very simple code:

import pandas as pd 

file_name = "C:/Users/Admin/Downloads/Results.csv"
df = pd.read_csv (file_name, sep=',')
print(df)

The file contains one single line: the header:

Publication,First Name,Last Name,Constituency,Caucus,Province,Date,Time,Page,Text

and the above code dispalys:

P   Unnamed: 1  Unnamed: 2  Unnamed: 3  Unnamed: 4  Unnamed: 5  Unnamed: 6  Unnamed: 7  Unnamed: 8  Unnamed: 9

So, it looks like pandas take the first character of publication and the other colums are displayed as unamed.

CodePudding user response:

  1. You can first try to open the csv file using Microsoft Excel (or similar apps). This is to validate the file is a valid csv file.

  2. Then you can try something like (assume you are using pandas 1.3.0 )

    pd.read_csv(file_name, sep=',', encoding='utf-8', encoding_errors='ignore')

More possible parameters: https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

CodePudding user response:

I don't know if this is a problem you have but when I tried to replicate this I started to have some problems loading in the file so I had to add double \\ instead of single \.

So the new path looks like file_name = "C:\\Users\\Admin\\Downloads\\Results.csv"

Other than that, I was unable to replicate the issue you describe.

  • Related