w3resource

Pandas Series: pop() function

Get item and drop from frame

The pop() function is used to get item and drop from frame. Raise KeyError if not found.

Syntax:

Series.pop(self, item)
Pandas Series pop() function

Parameters:

Name Description Type Required / Optional
item Label of column to be popped.

str Required

Returns: Series.

Example :

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([('eagle', 'bird', 320.0),
                   ('emu', 'bird', 48.0),
                   ('tiger', 'mammal', 120.5),
                   ('wolf','mammal', np.nan)],
                  columns=('name', 'class', 'max_speed'))
df

Output:

   name	  class	 max_speed
0	eagle	bird	320.0
1	emu	    bird	48.0
2	tiger	mammal	120.5
3	wolf	mammal	NaN
Pandas Series pop() function

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([('eagle', 'bird', 320.0),
                   ('emu', 'bird', 48.0),
                   ('tiger', 'mammal', 120.5),
                   ('wolf','mammal', np.nan)],
                  columns=('name', 'class', 'max_speed'))
df.pop('class')

Output:

0      bird
1      bird
2    mammal
3    mammal
Name: class, dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([('eagle', 'bird', 320.0),
                   ('emu', 'bird', 48.0),
                   ('tiger', 'mammal', 120.5),
                   ('wolf','mammal', np.nan)],
                  columns=('name', 'class', 'max_speed'))
df.pop('class')				  
df

Output:

   name	max_speed
0	 eagle	320.0
1	 emu	48.0
2	 tiger	120.5
3	 wolf	NaN
Pandas Series pop() function

Previous: Lazily iterate over tuples in Pandas
Next: Cross-section from the Series/DataFrame in Pandas



Follow us on Facebook and Twitter for latest update.