Pandas DataFrame | pop method
Start your free 7-days trial now!
Pandas DataFrame.pop(~)
method removes a single column of a DataFrame. The removal is done in-place, that is, the original DataFrame will be modified and no new DataFrame will be created.
The DataFrame.drop(~)
method provides a more flexible API. The drop(~)
method can do everything the pop(~)
can, but with far more flexibility:
you can remove not only columns, but also rows as well.
drop(~)
allows for multiple rows/columns removal, whereaspop(~)
only does this one at a time.
Parameters
1. item
| string
The name of the column you want removed.
Return Value
A Series
that holds the removed column values.
Examples
Consider the following DataFrame:
df = pd.DataFrame({"A":[1,2], "B":[3,4]})df
A B0 1 31 2 4
To remove column A
:
df.pop("A")
0 11 2Name: A, dtype: int64
Here, the deleted column is returned as a Series
.
The removal is performed in-place, that is, the df
will be modified directly without the creation of a new DataFrame. To confirm this, we check the state of the df
now that we've called pop(~)
:
df
B0 31 4
Notice how the column A
is now gone.