0

This code is giving me an error in Pandas:

df1 = pd.read_excel(file1,header=None, index_col=[0,1])

...gives this error:

Error: offset = 1 + header TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

If I have only one index col its working fine:

df1 = pd.read_excel(file1,header=None, index_col=0)

Or if I have multiple index column with header its working fine:

df1 = pd.read_excel(file1,index_col=[0,1])

So, how to define multiple index column with no header. The issue here is internally the code is trying to add 1 to header (which is None) in case of multiple index and its failing.

File content: row 1: p w 1 2 row 2: q x 3 4

smci
  • 32,567
  • 20
  • 113
  • 146
  • 1
    please put your `file1` content here. – Frank AK Jan 19 '18 at 05:01
  • 1
    Please add it into your question. No one can figure out where the spaces and newlines are like that. – cs95 Jan 19 '18 at 05:03
  • Sorry I am not able to figure out how to add table in question. There are simply 2 rows. I have added it in question. – Sidhant Shubham Jan 19 '18 at 05:11
  • Please post your file contents. Otherwise noone can answer you - we need to see the exact spaces, newlines and separators. Try opening in Excel and saving as CSV. – smci Aug 08 '19 at 10:42

3 Answers3

1

This is a bug from pandas that has been reported here. It will be fixed in pandas v1.1.

Note that this was happening independently of the file being read.

rafasan
  • 176
  • 8
0

Workaround: read_excel and create index separately:

  df1 = pd.read_excel(file1,header=None) 
  df1.set_index([0,1],inplace=True)

Still not sure how to do that in one line.

smci
  • 32,567
  • 20
  • 113
  • 146
  • Seems like there was a pandas parser error, but since you never showed us your file contents like multiple people asked you to, we can't see the exact spaces, newlines and separators, so this isn't a very reusable resource for other users. (Also, please state the pandas version, and retry with the current version and confirm if this still happens) – smci Aug 08 '19 at 10:45
-1

df1 = pd.read_excel(file1,header=None, index_col=0) should work in this case.

index_col=0 indicates that first column must be treated as index for your dataframe.

Refer: Different read_csv index_col = None / 0 / False in pandas

vindev
  • 2,240
  • 2
  • 13
  • 20
Janagaraj
  • 1
  • 2
  • Hi thanks for your answer. Yes its working fine for one index column. But what if I want multiple index column with no header. Is there any solution? – Sidhant Shubham Jan 19 '18 at 05:16
  • In this case I suppose you need to go for unstack() or pivot_table(). May be the below discussion would help you. I haven't tried this yet. https://stackoverflow.com/questions/41208610/set-multi-column-index-in-pandas – Janagaraj Jan 22 '18 at 07:01
  • This is not a solution to the problem. What the OP describes is a know bug from pandas reported here: https://github.com/pandas-dev/pandas/issues/31783 – rafasan Jul 03 '20 at 20:42