How to Drop Unnamed Column in Pandas DataFrame
Last Updated :
20 Mar, 2025
Pandas is an open-source data analysis and manipulation tool widely used for handling structured data. In some cases, when importing data from CSV files, unnamed columns (often labeled as Unnamed: X) may appear. These columns usually contain unnecessary data, such as row indices from previous exports.
This article explores various methods to remove unnamed columns from a Pandas DataFrame efficiently.
For the link to the CSV file Used in the Code, click student_data.csv file
Using the drop() function
drop() function removes specified columns or rows by providing label names. It can be used to drop unnamed columns from a DataFrame.
Syntax:
df.drop(df.columns[df.columns.str.contains('unnamed', case=False)], axis=1, inplace=True)
Parameters:
- df.columns.str.contains('unnamed', case=False): Identifies columns containing the text "unnamed" (case-insensitive).
- axis=1: Specifies that the operation should be applied to columns (instead of rows).
- inplace=True: Modifies the DataFrame in place without creating a copy.
Example:
Python
import pandas as pd
# Read the CSV file
df = pd.read_csv(
'https://media.geeksforgeeks.org/wp-content/uploads/20240208164753/student_data3.csv')
# Removing unnamed columns using drop function
df.drop(df.columns[df.columns.str.contains(
'unnamed', case=False)], axis=1, inplace=True)
print(df)
Output:
Actual dataframe:
name subject Unnamed: 2 fees fine
0 Arun Maths 9 9000 400
1 Aniket Social Science 10 12000 600
2 Ishita English 11 15000 0
3 Pranjal Science 12 18000 1000
4 Vinayak Computer 12 18000 500
Dataframe after removing unnamed columns:
name subject fees fine
0 Arun Maths 9000 400
1 Aniket Social Science 12000 600
2 Ishita English 15000 0
3 Pranjal Science 18000 1000
4 Vinayak Computer 18000 500
Explanation: drop() identifies columns containing "Unnamed" using df.columns.str.contains('unnamed', case=False). The axis=1 parameter ensures column-wise deletion, while inplace=True modifies the DataFrame directly without creating a copy.
Using the loc[] function
loc[] function is used to access groups of rows and columns by labels. It can be used to filter out unnamed columns.
Syntax:
df = df.loc[:, ~df.columns.str.contains('^Unnamed')]
Parameters:
- df.columns.str.contains('^Unnamed'): Finds all columns whose name starts with "Unnamed".
- ~: Negates the condition, selecting only the columns that do not match.
- loc[:, ...]: Selects all rows (:) and only the columns that do not contain "Unnamed".
Example:
Python
import pandas as pd
# Read the CSV file
df = pd.read_csv(
'https://media.geeksforgeeks.org/wp-content/uploads/20240208164753/student_data3.csv')
# Removing unnamed columns using drop function
df = df.loc[:, ~df.columns.str.contains('^Unnamed')]
print(df)
Output:
Actual dataframe:
name subject Unnamed: 2 fees fine
0 Arun Maths 9 9000 400
1 Aniket Social Science 10 12000 600
2 Ishita English 11 15000 0
3 Pranjal Science 12 18000 1000
4 Vinayak Computer 12 18000 500
Dataframe after removing unnamed columns:
name subject fees fine
0 Arun Maths 9000 400
1 Aniket Social Science 12000 600
2 Ishita English 15000 0
3 Pranjal Science 18000 1000
4 Vinayak Computer 18000 500
Explanation: This method filters out unnamed columns by selecting only those that do not match the pattern '^Unnamed'. The ~ operator negates the condition, ensuring that columns with "Unnamed" are excluded while keeping all rows intact.
Handling unnamed coloums while importing data
If unnamed columns appear due to exporting data with an index, we can specify the index_col=0 parameter while reading the CSV file. This method prevents Pandas from reading the index column as a separate unnamed column.
Syntax:
df=pd.read_csv(csv_file ,index_col=0)
Parameters:
- csv_file: The path or URL of the CSV file.
- index_col=0: Specifies that the first column should be treated as the index, preventing Pandas from interpreting it as an unnamed column.
Example:
Python
import pandas as pd
df1 = pd.DataFrame({'name': ['Arun', 'Aniket', 'Ishits', 'Pranjal', 'Vinayak'],
'subject': ['Maths', 'Social Science', 'English', 'Science', 'Computer'],
'fees': [9000, 12000, 15000, 18000, 18000]})
# Store the data frame in a CSV file
df1.to_csv('student_data.csv')
# Read the CSV file
df = pd.read_csv('student_data.csv')
# Read the CSV file removing unnamed columns
df = pd.read_csv('student_data.csv', index_col=0)
print(df)
Output:
Actual dataframe:
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
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
Explanation: If unnamed columns appear due to index inclusion during CSV exports, specifying index_col=0 in pd.read_csv() prevents Pandas from treating the first column as a separate unnamed column. This ensures a cleaner DataFrame without unnecessary columns.
Similar Reads
How to rename columns in Pandas DataFrame
In this article, we will see how to rename column in Pandas DataFrame. The simplest way to rename columns in a Pandas DataFrame is to use the rename() function. This method allows renaming specific columns by passing a dictionary, where keys are the old column names and values are the new column nam
4 min read
How to Get Column Names in Pandas Dataframe
While analyzing the real datasets which are often very huge in size, we might need to get the pandas column names in order to perform certain operations. The simplest way to get column names in Pandas is by using the .columns attribute of a DataFrame. Let's understand with a quick example: [GFGTABS]
4 min read
How to drop one or multiple columns in Pandas Dataframe
Let's learn how to drop one or more columns in Pandas DataFrame for data manipulation. Drop Columns Using df.drop() MethodLet's consider an example of the dataset (data) with three columns 'A', 'B', and 'C'. Now, to drop a single column, use the drop() method with the columnâs name. [GFGTABS] Python
4 min read
How to add column from another DataFrame in Pandas ?
In this discussion, we will explore the process of adding a column from another data frame in Pandas. Pandas is a powerful data manipulation library for Python, offering versatile tools for handling and analyzing structured data. Add column from another DataFrame in Pandas There are various ways to
6 min read
How to Get First Column of Pandas DataFrame?
In this article, we will discuss how to get the first column of the pandas dataframe in Python programming language. Method 1: Using iloc[] function This function is used to get the first column using slice operator. for the rows we extract all of them, for columns specify the index for first column
4 min read
How to Set Cell Value in Pandas DataFrame?
In this article, we will discuss how to set cell values in Pandas DataFrame in Python. Method 1: Set value for a particular cell in pandas using dataframe.at This method is used to set the value of an existing value or set a new record. C/C++ Code # import pandas module import pandas as pd # create
2 min read
How to Drop Negative Values in Pandas DataFrame
Handling data effectively is crucial in data analysis and manipulation. One common task is cleaning the data by removing negative values, especially when they are considered outliers or invalid entries. The Pandas library in Python offers several efficient methods to accomplish this. This article wi
3 min read
How to Subtract Two Columns in Pandas DataFrame?
In this article, we will discuss how to subtract two columns in pandas dataframe in Python. Dataframe in use: Method 1: Direct Method This is the __getitem__ method syntax ([]), which lets you directly access the columns of the data frame using the column name. Example: Subtract two columns in Panda
3 min read
How to Access a Column in a DataFrame with Pandas
In this article we will explore various techniques to access a column in a dataframe with pandas with concise explanations and practical examples. Method 1: Accessing a Single Column Using Bracket NotationBracket notation is the most straightforward method to access a column. Use the syntax df['colu
4 min read
How to take column-slices of DataFrame in Pandas?
In this article, we will learn how to slice a DataFrame column-wise in Python. DataFrame is a two-dimensional tabular data structure with labeled axes. i.e. columns. Creating Dataframe to slice columns[GFGTABS] Python # importing pandas import pandas as pd # Using DataFrame() method from pandas modu
2 min read