This tutorial will explore various techniques we can use to remove unnamed columns in a Pandas DataFrame.
Let us go exploring.
In the code below, we have a sample CSV file that holds a sample DataFrame you can use to follow along with this guide.
The CSV is as shown:
Start by importing pandas as shown below:
import pandas as pd
Next, load the csv file as shown in the example code below:
df = pd.read_csv('sample.csv')
This should load the csv file into a DataFrame as shown below:
You will note that the DataFrame has an unnamed column.
Drop Unnamed Column Using drop() Function
To remove an unnamed column in Pandas, we can use the drop function as shown below:
df.drop("Unnamed: 0", axis=1)
The code above should remove the column “Unnamed: 0” from the DataFrame.
This should return:
This article discussed how to remove an unnamed column from a Pandas DataFrame using the drop function. Check the docs to learn more.