np.genfromtxt value error on python (with csv. file)

71 Views Asked by At
current_account = np.genfromtxt(fname = "q3_1_current_account.csv",delimiter = ",")
current_account

I want to upload a csv file to my python, but it makes an error.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[10], line 3
      1 # your code here
      2 # q3_1
----> 3 current_account = np.genfromtxt(fname = "q3_1_current_account.csv",delimiter = ",")
      4 current_account

File /opt/conda/lib/python3.9/site-packages/numpy/lib/npyio.py:2145, in genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows, encoding, like)
   2143 # Raise an exception ?
   2144 if invalid_raise:
-> 2145     raise ValueError(errmsg)
   2146 # Issue a warning ?
   2147 else:
   2148     warnings.warn(errmsg, ConversionWarning, stacklevel=2)

ValueError: Some errors were detected !
    Line #2 (got 501 columns instead of 280)
    Line #3 (got 507 columns instead of 280)
    Line #4 (got 388 columns instead of 280)
    Line #5 (got 375 columns instead of 280)
    Line #6 (got 282 columns instead of 280)

I have to use np.genfromtxt to do my task. Can you help me what is wrong with my code?

(The picture below is the part of csv file. And there are no any blank values.) enter image description here

1

There are 1 best solutions below

1
On

Looking at screenshot you provided I smell that np.genfromtxt() chokes on some data from your header row (like these in very first columns), so I'd skip that row with use of skip_headers:

current_account = np.genfromtxt(fname="q3_1_current_account.csv", delimiter=",", skip_header=1)