ADVERTISEMENT
ADVERTISEMENT

How to get rid of 'Unnamed: 0' column in a pandas DataFrame read in from CSV file?

Solution for the situations when an Unnamed: 0 column in pandas comes when you are reading CSV file.
Submitted by Pranit Sharma, on May 23, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consists of rows, columns, and the data.

DataFrame can be created with the help of python dictionaries or lists but in the real world, CSV files are imported and then converted into DataFrames. Sometimes, DataFrames are first written into CSV files. Here, we are going to write a DataFrame into a CSV file.

CSV files or Comma Separated Values files are plain text files but the format of CSV files is tabular. As the name suggests, in a CSV file, each specific value inside the CSV file is generally separated with a comma. The first line identifies the name of a data column. The further subsequent lines identify the values in rows.

col_1_value, col_2_value ,  col_3_value
row1_value1 , row_1_value2 , row_1_value3
row1_value1 , row_1_value2 , row_1_value3

Here, the separator character (,) is called the delimiter. There are some more popular delimiters. E.g.: tab(\t), colon (:), semi-colon (;) etc.
Sometimes, while reading a CSV file, we get an unnamed column which is in the form of 'Unnamed: 0'. We do not need this unwanted column hence we need to drop this column. Here, we are going to learn how to get rid of this unwanted column?

Let us understand with the help of an example,

# Importing pandas package
import pandas as pd

# Importing dataset
data=pd.read_csv('C:/Users/hp/Desktop/Includehelp/mycsv1.csv')

# Print the dataset
print(data)

Output:

Example 1: get rid of 'Unnamed: 0' column

Here, we can observe the red arrow shows that we have an unwanted column named 'unnamed:0', now we need to pass index_col=[0] which will drop this unwanted column.

# Importing Pandas package
import pandas as pd

# Importing dataset
data=pd.read_csv('C:/Users/hp/Desktop/Includehelp/mycsv1.csv', index_col=[0])

# Print the dataset
print(data)

Output:

Example 2: get rid of 'Unnamed: 0' column

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.