Pandas Tutorial Pandas References

Pandas Series - pop() function



The Pandas Series pop() function is used to drop the element of specified label from the series. If the specified label is not found in the series, it raises KeyError exception.

Syntax

Series.pop(item)

Parameters

item Required. Specify the label (index) of the element that needs to be removed.

Return Value

Returns the element which is popped from the series.

Example:

In the example below, pop() function is used to delete the specified element from the given series.

import pandas as pd
Name = ['John', 'Marry', 'Jo', 'Sam']
x = pd.Series(Name)
print(x)

#pop the element with label=1
x.pop(1)
print("\nAfter popping element:")
print(x)

The output of the above code will be:

0     John
1    Marry
2       Jo
3      Sam
dtype: object

After popping element:
0    John
2      Jo
3     Sam
dtype: object

Example:

In this example, the series contains provided index.

import pandas as pd
Name = ['John', 'Marry', 'Jo', 'Sam']
x = pd.Series(Name, index=['P1', 'P2', 'P3', 'P4'])
print(x)

#pop the element with label='P3'
x.pop('P3')
print("\nAfter popping element:")
print(x)

The output of the above code will be:

P1     John
P2    Marry
P3       Jo
P4      Sam
dtype: object

After popping element:
P1     John
P2    Marry
P4      Sam
dtype: object

❮ Pandas Series - Functions