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)
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
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
Previous: Lazily iterate over tuples in Pandas
Next: Cross-section from the Series/DataFrame in Pandas
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-pop.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics