Pandas DataFrame pop() Function:
Dataframe pop: The pop() function of the Pandas DataFrame removes the given column from the DataFrame and returns the removed columns as a Pandas Series object. If the given item is not found, the function throws a KeyError exception.
Syntax:
DataFrame.pop(label)
Parameters:
label: This is required. It indicates the label of the column that has to be removed.
Return Value:
Pandas pop: The pop() function of the Pandas DataFrame returns the removed column, as a Pandas Series object.
- Python Pandas DataFrame items() Function
- Python Pandas DataFrame iteritems() Function
- Python Pandas DataFrame itertuples() Function
Pandas DataFrame pop() Function in Python
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe
- Drop/remove some random column from the dataframe using the pop() function by passing the column name as an argument to it.
- Here we removed the “emp_age” column from the dataframe
- Print the altered dataframe after the removal of “emp_age” column.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random key-value pair(dictionary), index list as arguments to the # DataFrame() function of the pandas module to create a dataframe # Store it in a variable. data_frme = pd.DataFrame({ "emp_name": ["john", "nick" , "jessy", "mary"], "emp_age": [25, 35, 38, 22], "emp_salary": [25000, 40000, 22000, 80000]}, index= [1, 2, 3, 4] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Drop/remove some random column from the dataframe using the pop() function # by passing the column name as an argument to it. # Here we removed the "emp_age" column from the dataframe data_frme.pop("emp_age") # Print the altered dataframe after the removal of "emp_age" column print("The altered dataframe after the removal of 'emp_age' column:") print(data_frme)
Output:
The given Dataframe: emp_name emp_age emp_salary 1 john 25 25000 2 nick 35 40000 3 jessy 38 22000 4 mary 22 80000 The altered dataframe after the removal of 'emp_age' column: emp_name emp_salary 1 john 25000 2 nick 40000 3 jessy 22000 4 mary 80000