4

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!!

RakeshKalwa
  • 671
  • 2
  • 6
  • 16

3 Answers3

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.

bigmac
  • 145
  • 1
  • 6
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)

sygneto
  • 1,761
  • 1
  • 13
  • 26
  • 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? – vinodhraj Apr 21 '22 at 00:34
1

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)

007mrviper
  • 459
  • 4
  • 20