0

I am using the pandas function read_csv to read a CSV with no index column.

read_csv("file.csv", header=1)

I was expecting that PANDAS would generate an index for each row based on the documentation

index_col: column number, column name, or list of column numbers/names, to use as the index (row labels) of the resulting DataFrame. By default, it will number the rows without using any column, unless there is one more data column than there are headers, in which case the first column is taken as the index.

However, while loading the file it throws,

Exception: Reindexing only valid with uniquely valued Index objects

And I cannot figure out why this would be the case. What causes this exception?

I have also tried passing skiprows and nrows and the same exception occurs.

4
  • Can you give an example of the data? Commented Sep 18, 2012 at 18:51
  • it's all over the place. strings, ints, floats, dates. But I am not trying to parse any of it to a particular data type. Commented Sep 18, 2012 at 18:53
  • I mean you need to give a specific example. Like, provide a sample data file that actually causes the error. The problem almost certainly has to do with the specifics of how that file is formatted and what data it contains. Commented Sep 18, 2012 at 18:55
  • Sorry @BrenBarn I didn't feel comfortable posting my data. Commented Sep 18, 2012 at 19:05

1 Answer 1

1

The problem is that the header argument is not a True/False type argument. Rather, it specifies the row number for the header. Since it is specified as 1 it is using the 2nd row for the header and considering that this row contains actual data, the values are not necissarily unique.

Changing the command to

read_csv("file.csv")

or

read_csv("file.csv", header=0)

fixes the problem. It is such a "duh" moment but being used to R I mistakenly thought header=1 was specifying to read a header. Ugh.

For future reference, the Exception

Reindexing only valid with uniquely valued Index objects

relates to the header values not being unique.

Sign up to request clarification or add additional context in comments.

1 Comment

What version of pandas are you using? read_csv seemed to work ok on a few toy examples I came up with using non-unique headers. I don't know if you can come up with a specific data set that reproduces the issue without divulging data you want to keep private.

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.