Pandas DataFrame pop() method with examples

Spread the love

The pop() method in pandas is used to delete a column from a dataframe. The column name that is passed to the function is dropped from the dataframe and returned as series.

Syntax –

dataframe.pop(column_name)

Example –

Let’s read a dataset to work with.

import pandas as pd

url="https://raw.githubusercontent.com/bprasad26/lwd/master/data/winequality-red.csv"
df = pd.read_csv(url)
df.head()

Now, Let’s say you want to delete the pH column. For that you can use the pop method.

# delete the pH column
ph = df.pop('pH')
ph.head()

Now if you check the dataframe you will see that pH column has been deleted.

df.head()

Rating: 1 out of 5.

Leave a Reply