12

I have a very simple table in Excel that I'm trying to read into a DataFrame

Excel data

Code:

from pandas import DataFrame, Series
import pandas as pd

df = pd.read_excel('params.xlsx', header=[0,1], index_col=None)

This results in the following DataFrame:

DataFrame

I didn't expect param1.key to become the index, especially after having set index_col=None. Is there a way to get the data into a DataFrame with a generated index instead of the data from the first column?

Update — here's what happens when you try reset_index() to resolve the issue:

DataFrame after reset_index()

Version info:

  • Python 3.5.0
  • pandas (0.17.1)
  • xlrd (0.9.4)
5
  • 1
    This seems like a bug to me. In any case, you can always do reset_index() to remediate. Commented Dec 1, 2015 at 12:10
  • Reported it here: github.com/pydata/pandas/issues/11733 Commented Dec 1, 2015 at 12:30
  • @joris Thanks for reporting the bug. I tried the reset_index() before posting, thinking it would solve the issue. While the index is indeed reset, the labels are still messed up (see screenshot in updated question). Commented Dec 1, 2015 at 12:37
  • Ah, yes, indeed, it sees the ('param1', 'key') as the level names of the columns .. Do you have a large dataframe? I can try to come up with some complicated code to clean this up automatically, but if it is rather small dataframe, probably easier to just put the correct column names by hand Commented Dec 1, 2015 at 12:41
  • @joris It's a small one, no worries. Thanks for the offer though. Commented Dec 1, 2015 at 12:42

2 Answers 2

5

You could use this:

# just use col order number in the `index_col` field, 0-indexed
df = pd.read_excel('yourFile.xlsx',index_col=0)
Sign up to request clarification or add additional context in comments.

Comments

3

It seems like a bug. You can get a column out of your index by simply doing:

df['columnName'] = df.index

Comments

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.