How to Remove the Index Column in Pandas While Reading from the CSV file – Definitive Guide

Pandas index columns allow identifying each row uniquely.

You can remove the index column in pandas while reading from the CSV file using the parameter index_col=None parameter.

  • By default, the read_csv() method doesn’t make any columns as an index.
  • While reading the CSV file, you have to specify if a column must be made an index column.
  • Otherwise, the default arithmetic sequence number will be made as an index of the dataframe. An index is mandatory, and this cannot be removed.

You can drop the index using the following code if the file is read with an index.

Syntax

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

df

Remove Index Using read_CSV and index_col Parameter

By default, the read_csv() method uses the arithmetic sequence as an index.

  • Pass index_col=None to explicitly specify no columns from the CSV file must be made an index.
  • The numbers starting from 0..n will be made as an index of the pandas dataframe.

Code

import pandas as pd

df = pd.read_csv('addresses.csv', index_col=None,  header=None)

df

DataFrame Will Look Like

0123456
0a-1JohnDoe120 jefferson st.RiversideNJ8075
1a-2VikramAruchamyChepauk, second streetCoimbatoreTamilNadu600100

Dropping Index Column

This section teaches you how to read a CSV file with an index column and drop them later.

  • Use the parameter index_col=0 to make the first column in the CSV file an index.

Code

import pandas as pd

df = pd.read_csv('addresses.csv', index_col=0,  header=None)

df

DataFrame Will Look Like

123456
0
a-1JohnDoe120 jefferson st.RiversideNJ8075
a-2VikramAruchamyChepauk, second streetCoimbatoreTamilNadu600100

Once you have the dataframe, you can use the reset_index() method to reset the existing index.

  • Use drop=True to drop the existing index
  • Use inplace=True to perform the drop operation in the same dataframe instead of creating a new dataframe

Code

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

df

DataFrame Will Look Like

123456
0JohnDoe120 jefferson st.RiversideNJ8075
1VikramAruchamyChepauk, second streetCoimbatoreTamilNadu600100

Additional Resources

Leave a Comment