Q:

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

belongs to collection: Python Pandas Programs

0

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?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to read a large CSV file with pandas?... >>
<< How to determine whether a Pandas Column contains ...