drop column pandas dataframe | drop multiple columns in pandas examples
In this pandas tutorial, we will discuss about:
-
drop column pandas dataframe,
-
drop column in pandas example,
-
drop multiple columns in pandas,
-
drop multiple columns pandas using index
DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key.
Example: Create DataFrame
In this example, we will create dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: Dataframe is created below
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
Now lets drop column pandas dataframe.
Drop column pandas dataframe
Lets see few ways we can drop column in pandas.
Method 1 : drop column in pandas using drop() with column label
Here, we will use drop() function to remove/drop the columns from the given dataframe. We have to specify the column name/label to drop particular column.
So based on the label, it will drop particular column.
Syntax:
dataframe.drop('column_label',axis=1)
where,
-
dataframe is the input pandas DataFrame.
-
column_label specifies the column name.
-
axis=1 specifies the column.
Example: drop column in pandas demo
In this drop column in pandas example, we will display the dataframe by dropping college_id column.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#display the dataframe by dropping college_id column
print(data.drop('college_id',axis=1))
Output: drop column in pandas result where college_id column is dropped.
college_name college_address Total Staff
one vignan university guntur 1200
two vvit guntur 3422
three RVR - JC guntur 5644
four Andhra University guntur 670
In the above code, we are removing/ dropping college_id column and displaying the entire dataframe.
Drop multiple columns in pandas
We can also drop multiple columns at a time. Just pass a list of column names to be removed inside drop() function.
Example: drop column in pandas example 2
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#display the dataframe by dropping college_id, college_address and college_name columns
print(data.drop(['college_id','college_address','college_name'],axis=1))
Output: drop multiple columns in pandas result is given below
Total Staff
one 1200
two 3422
three 5644
four 670
In the above code, we are removing/ dropping college_id,college_address,college_name columns and displaying the entire dataframe.
This drop column in pandas example shows how we can drop multiple columns from dataframe at same time.
Method 2 : drop column in pandas using drop() with column position
Here, we will use drop() function to remove/drop the column from the given dataframe. We have to specify the column position to drop particular column.
column position starts with 0.
So based on the position, it will drop particular column. We can use dataframe.columns method to represent the column position.
Syntax:
dataframe.drop(dataframe.columns[column_position],axis=1)
where,
-
dataframe is the input pandas DataFrame.
-
column_position parameter specifies the column position in columns method.
-
axis =1 specifies the column.
Example: drop column in pandas by column position
In this example, we will display the dataframe by dropping college_id column.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#display the dataframe by dropping college_id column
print(data.drop(data.columns[0],axis=1))
Output: drop column in pandas result is given below
college_name college_address Total Staff
one vignan university guntur 1200
two vvit guntur 3422
three RVR - JC guntur 5644
four Andhra University guntur 670
From the above code, the output will be college_name, college_address and Total Staff columns data
because, we dropped the college_id column.
drop multiple columns in pandas using column position
We can also drop multiple columns in pandas at a time. Just pass a list of column positions to be removed inside drop() function.
Example: drop multiple columns in pandas by specifying the respective column position is given below
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
},index=['one','two','three','four'])
#display the dataframe by dropping college_id, college_address and college_name columns
print(data.drop([data.columns[0],data.columns[1],data.columns[2]],axis=1))
Output: drop multiple columns pandas using index result is given below
Total Staff
one 1200
two 3422
three 5644
four 670
From the above code, the output will be Total Staff column data because, we dropped the college_id,college_name and college_address columns.
Thus we have seen drop column in pandas and drop multiple columns in pandas examples as well.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1200+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Mar 14,2022