1

Today I turn my attention to the wonderful world of pandas. I am trying to read a .csv file, which is an easy task, but when I pass in the file (right here: https://raw.githubusercontent.com/grammakov/USA-cities-and-states/master/us_cities_states_counties.csv), it gives me this mess:

pandas.errors.ParserError: Error tokenizing data. C error: Expected 4 fields in line 9, saw 5

I have tried setting delimiter to an empty string, setting it to a variable, and declaring the type pd.Dataframe.

8
  • 1
    Show ur csv file. It clearly says in the ParseError that the problem is in the CSV and not in ur code Commented Nov 24, 2020 at 1:28
  • 1
    THe error is saying the csv is the problem, but I am not so sure now Commented Nov 24, 2020 at 1:29
  • 2
    "setting delimiter to an empty string" is your problem. The file you linked is delimited with a pipe character, '|', so when you use anything else it parses it weird Commented Nov 24, 2020 at 1:31
  • 1
    yup I just realized that too man Commented Nov 24, 2020 at 1:32
  • 1
    yep pretty much Commented Nov 24, 2020 at 1:33

1 Answer 1

3

Use delimiter |, by adding either sep='|' or delimiter='|' to .read_csv() (both parameters mean the same thing). like

pd.read_csv(r'File_path', sep='|')
Sign up to request clarification or add additional context in comments.

3 Comments

@Arandomcoder i ran this code from pandas import read_csv as rc >>> rc("data.csv",sep='|') , it did work
The delimiter and sep parameters are synonymous. Use one or the other but not both.
@tdelaney yes, just used sep for brevity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.