5

We are trying to read a sample simple csv file using pandas in python as follows -

df = pd.read_csv('example.csv')

print(df)

enter image description here

We need df by removing below red highlighted index column -

enter image description here

We have tried multiple ways by passing parameters but no luck.

Please help me in this issue!!

1
  • what do you mean by remove index? Pandas will naturally assign an index if none is supplied. Commented Aug 13, 2020 at 8:59

3 Answers 3

4

A dataframe requires having some kind of index as part of the structure.

If you want to simply print the output without the index you can use the approach suggested here, with Python 3 syntax:

print(df.to_string(index=False))

but it will not have the nice dataframe rendering in Jupyter as you have in your example.

If you want to avoid pandas outputting the index when writing to a CSV file you can use the option index=False, for example:

df.to_csv('example.csv', index=False)

This will avoid creating the index column in the saved CSV file.

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

Comments

4

I had the same issue, I tried index_col=False, and index_col=None, but none worked.
But index_col=0 worked.

So do like this when reading a file.
df = pd.read_csv('yourfilename.csv', index_col=0)

Comments

3

add index_col=False

pd.read_csv('path.csv',index_col=False)

or remove index from dataframe

df.reset_index(drop=True, inplace=True)

1 Comment

setting index_col=False worked for me. However, I got a warning that Length of header or names does not match the length of data. Any idea what's that about?

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.