Pandas Tutorial Pandas References

Pandas DataFrame - pop() function



The Pandas DataFrame pop() function returns item and drop from frame. The function raises KeyError exception if the specified item is not found.

Syntax

DataFrame.pop(item)

Parameters

item Required. Specify the label of column to be popped.

Return Value

Returns the popped Series.

Example:

In the example below, a DataFrame df is created. The pop() function is used to drop the specified column from the DataFrame.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Last Salary": [58, 60, 63, 57],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#dropping the Last Salary column
df.pop("Last Salary")

print("\nThe modified DataFrame is:")
print(df)

The output of the above code will be:

The DataFrame is:
       Bonus  Last Salary  Salary
John       5           58      60
Marry      3           60      62
Sam        2           63      65
Jo         4           57      59

The modified DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

❮ Pandas DataFrame - Functions

5