How to Delete a Column From a Pandas DataFrame in Python
Python is a great programming/data science/statistical language for doing data analysis having it’s huge data-centric packages. Pandas is one of those data-centric packages for analyzing data much in easier way.
Pandas provides data analysts/data scientist different options to delete a column using .drop() method. Columns can be removed using index label or column name using this method. In this article, I’ll show you various approaches to delete a column from a Pandas DataFrame in Python. I have an another article How to Create a Pandas DataFrame in Python, where you will learn how to create a Pandas DataFrame in Python.
Syntax of drop Method:
DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=’raise’)
Parameters of drop Method:
labels: String or list of strings referring row or column name.
axis: int or string value, 0 ‘index’ for Rows and 1 ‘columns’ for Columns.
index or columns: Single label or list. index or columns are an alternative to axis and cannot be used together.
level: Used to specify level in case data frame is having multiple level index.
inplace: Makes changes in original Data Frame if True.
errors: Ignores error if any value from the list doesn’t exists and drops rest of the values when errors = ‘ignore’
Return Type of drop Method: DataFrame with dropped values
For this tutorial, create a sample DataFrame as below:
id name math_score 0 202301 Minhaj 92 1 202302 Ridhwan 86 2 202303 Tanveer 76 3 202304 Sharodia 89 4 202305 Alve 99 5 202306 Intisar 99
Now, let’s see some examples to delete a column from a Pandas DataFrame.
Option-1: Using drop method and labels, axis, inplace parameters
df.drop(labels=["math_score"],axis=1,inplace=True)
print(df)
Output:
id name 0 202301 Minhaj 1 202302 Ridhwan 2 202303 Tanveer 3 202304 Sharodia 4 202305 Alve 5 202306 Intisar
Option-2: Using drop method and labels & axis parameter with multiple columns
df=df.drop(labels=["name","math_score"],axis=1)
print(df)
Output:
id 0 202301 1 202302 2 202303 3 202304 4 202305 5 202306
Option-3: Using drop method and columns, axis parameters (Multiple Column Name)
df=df.drop(columns=["name","math_score"],axis=1)
print(df)
Output:
id 0 202301 1 202302 2 202303 3 202304 4 202305 5 202306
Option-4: Using drop method and columns, column index, axis parameters (Single Index)
df=df.drop(columns=df.columns[2],axis=1)
print(df)
Output:
id name 0 202301 Minhaj 1 202302 Ridhwan 2 202303 Tanveer 3 202304 Sharodia 4 202305 Alve 5 202306 Intisar
Option-5: Using drop method, columns, column index, axis parameters (Multiple Index)
df=df.drop(columns=df.columns[[1,2]],axis=1)
print(df)
Output:
id 0 202301 1 202302 2 202303 3 202304 4 202305 5 202306
In this tutorial, I tried to brief how to delete a column from Pandas DataFrame in Python. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page https://www.facebook.com/LearningBigDataAnalytics and stay connected.