0

I'm trying to run a python file on the command prompt, but keep getting this error.

Error: pandas.errors.ParserError: Expected 1 fields in line 42, saw 2

Line: df = pd.read_csv(df, encoding = 'unicode_escape', engine ='python')

I've tried changing the encoding to 'UTF-8',and changing the engine to 'c'. None have worked.

  • Seems like you generally have bad lines in that csv, with some lines not having all columns (maybe a trailing row?). Try error_bad_lines=False and see if the read in data looks good https://stackoverflow.com/questions/33440805/pandas-dataframe-read-csv-on-bad-data – Brandon Sollins Mar 24 '23 at 00:09
  • Are you sure you're using the correct delimiter? What's the first line of the CSV? – Nick ODell Mar 24 '23 at 00:17
  • The first line of the CSV are headers. Some rows have missing values in some columns. – Emily Aleman Mar 24 '23 at 01:00
  • It's okay if _values_ are missing, but the _column itself_ still needs to be there, i.e. there must be a comma to act as a placeholder for the empty value. – John Gordon Mar 24 '23 at 01:20
  • Although in this case, it seems that the problem is _too many_ columns, not too few. – John Gordon Mar 24 '23 at 01:22
  • df is a table exported directly from excel as a csv file. Do I need to fill in every missing value with a comma or 0 in excel? – Emily Aleman Mar 24 '23 at 01:30

1 Answers1

0

It sounds like you have a malformed table, or one that needs some manual adjustment before loading with pandas. Check for irregularity in the table at line 42! If the problem isn't apparent, some context would be helpful (i.e. what type of file is df, what are the columns, what does a healthy line look like, what does line 42 look like, etc.)

  • df is a CSV file from excel. It contains TESS data from the NASA Exoplanet Archive. Some rows have missing values for some columns. Line 42 appears to have no apparent problem and it has all values for its columns. Majority of columns are numbers, with only 1 column having an alphabetical value. – Emily Aleman Mar 24 '23 at 01:08
  • It looks like I'm able to reproduce a similar parser error with TESS data (CTOIs in this case, from https://exofop.ipac.caltech.edu/tess/view_ctoi.php), but using the default engine poses no problems. Is engine='python' necessary for you? – quinn maloney Mar 27 '23 at 01:34