4

See example csv file below:

A,B,C 
d,e,f 
g,h,i 

The first row with the capital letters are my headings.

I tried this:

df = pd.read_csv("example.csv", header=0, sep=",", index_col=0, parse_dates=True)

And the data frame that is created looks like this with the headings messed up.

  B C 
A 
d e f
g h i

Anyone know why or how I can fix this manually?

1 Answer 1

7

The issue is that when you pass index_col=0 argument to read_csv() , it takes the 0th column as the index column, hence in your resulting DataFrame, A is the index.

If you do not want to take A as the index, you should just omit the index_col=0 argument. Example -

df = pd.read_csv("example.csv", parse_dates=True)

I removed some other keyword arguments as well -

  • header=0 , header is 0 by default if names argument is not passed.
  • sep=',' , seperator is ',' by default.
Sign up to request clarification or add additional context in comments.

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.