0

I am trying to load this data-frame (df):

3 4
5 7 4 2 4
1 2 1 2 1

It is important to mention that shape of the df is 4,5 ... in other words 4 rows and 5 columns, but in the first row some values are missing. The df is space separated.

I am trying to read this table like this:

df = genfromtxt('table')

but getting this error:

ValueError: Some errors were detected ! Line #2 (got 5 columns instead of 2) Line #3 (got 5 columns instead of 2)

Is there any way to tell python that this df contains 5 columns and that some data are missing?

I try the similar with:

df = pd.read_table('table', delim_whitespace=True, header=None)

but getting this error:

ParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 5

In this case the question is similar, is there any way to tell python (this time using pandas) that my df has 5 columns?

user16454053
  • 101
  • 1
  • 9
  • possible duplicate: https://stackoverflow.com/questions/27020216/import-csv-with-different-number-of-columns-per-row-using-pandas you could do like this answer and put 5 instead of 8 columns: https://stackoverflow.com/a/60247230/8981483 – PaNh May 09 '22 at 17:06
  • 1
    Does this answer your question? [Read CSV into a dataFrame with varying row lengths using Pandas](https://stackoverflow.com/questions/55129640/read-csv-into-a-dataframe-with-varying-row-lengths-using-pandas) – Ynjxsjmh May 11 '22 at 05:17

0 Answers0