Open In App

Read CSV File without Unnamed Index Column in Python

Last Updated : 06 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Whenever the user creates the data frame in Pandas, the Unnamed index column is by default added to the data frame. The article aims to demonstrate how to read CSV File without Unnamed Index Column using index_col=0 while reading CSV.

Read CSV File without Unnamed Index Column Using index_col=0 while reading CSV

The way of explicitly specifying which column to make as the index to the read_csv() function is known as the index_col attribute. This method is useful if you have created the dataset in Pandas and have stored that in a CSV file. Then, while importing that CSV file back in Pandas, you can use this method, with syntax:

df=pd.read_csv(csv_file ,index_col=0)

In this method, we will use the Pandas data frame with three columns, name, subject and fees, Dataset link. On uploading this Pandas data frame in CSV, it by defaults shows an unnamed column in the dataset. The original data is:

Python3




import pandas as pd
 
# Read the CSV file into a DataFrame
df = pd.read_csv("student_data.csv")
 
# Display the DataFrame
print(df)


Output:

   Unnamed: 0     name         subject   fees
0           0     Arun           Maths   9000
1           1   Aniket  Social Science  12000
2           2   Ishits         English  15000
3           3  Pranjal         Science  18000
4           4  Vinayak        Computer  18000

Now, let’s see how to remove that unnamed column by using index_col attribute of read_csv function.

Python3




# Read the CSV file removing unnamed columns
df = pd.read_csv('student_data.csv', index_col=0)
print('Dataframe after removing unnamed columns:')
print(df)


Output:

Dataframe after removing unnamed columns:
      name         subject   fees
0     Arun           Maths   9000
1   Aniket  Social Science  12000
2   Ishits         English  15000
3  Pranjal         Science  18000
4  Vinayak        Computer  18000


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads