Articles

pandas DataFrame - pop()

pandas DataFrame - pop()


DataFrame in pandas is two dimensional data structure that store data in 2-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.

 

In this pandas tutorial, we will discuss about pop() function.

Introduction

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.

 

ExampleCreate pandas 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: Our dataframe is created

      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 have a look at pop().

This method is used to remove the column from the dataframe. It will return the column to be popped and remove the column name from the dataframe

Syntax:

dataframe.pop('column_name')

Example 1:

In this example, we will remove college_name 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'])

#pop the college_name column
data.pop('college_name')

#display the dataframe
data

Output:

In the above example, we popped the college_name column and remaining dataframe is returned.

   college_id	college_address	Total Staff
one	c-001	         guntur	       1200
two	c-021	         guntur	       3422
three	c-002	         guntur	       5644
four	c-004	         guntur	        670

Example 2:

In this example, we will remove 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'])

#pop the college_id
data.pop('college_id')

#display the dataframe
print(data)

Output:

In the above example, we popped the college_id column and remaining dataframe is returned.

            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

 


Pandas

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 : Apr 21,2023  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!