Pandas DataFrame.pop
Pandas DataFrame.pop
The DataFrame.pop
method in pandas is used to remove a specific column from a DataFrame and return it as a Series. This is particularly useful when you want to extract and remove a column from a DataFrame in one step.
Syntax
The syntax for DataFrame.pop
is:
DataFrame.pop(item)
Here, DataFrame
refers to the pandas DataFrame, and item
specifies the column label to be removed and returned.
Parameters
Parameter | Description |
---|---|
item | The column label (as a string) to be removed and returned. |
Returns
A pandas Series representing the removed column.
Examples
Removing a Column
Use the pop
method to remove a column from a DataFrame and return it as a Series.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Remove the 'Age' column using pop
print("Removing 'Age' column:")
age_series = df.pop('Age')
print("Extracted Column:")
print(age_series)
print("\nDataFrame after removing 'Age':")
print(df)
Output
Removing 'Age' column:
Extracted Column:
0 25
1 30
2 35
Name: Age, dtype: int64
DataFrame after removing 'Age':
Name Salary
0 Arjun 70000
1 Ram 80000
2 Priya 90000
Using the Removed Column
The returned Series can be reused for further operations.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Remove the 'Salary' column and calculate its mean
salary_series = df.pop('Salary')
print("Mean of the 'Salary' column:")
print(salary_series.mean())
Output
Mean of the 'Salary' column:
80000.0
Attempting to Remove a Non-Existent Column
If you try to remove a column that doesn’t exist, a KeyError
is raised.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Attempt to pop a non-existent column
try:
df.pop('Department')
except KeyError as e:
print("Error:", e)
Output
Error: 'Department'
Combining pop
with Other Operations
The pop
method can be combined with other pandas operations to streamline workflows.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Pop a column and add it as a new column after transformation
age_series = df.pop('Age')
df['Age in Months'] = age_series * 12
print("DataFrame after transforming and adding 'Age' back:")
print(df)
Output
DataFrame after transforming and adding 'Age' back:
Name Salary Age in Months
0 Arjun 70000 300
1 Ram 80000 360
2 Priya 90000 420
Summary
In this tutorial, we explored the DataFrame.pop
method in pandas. Key takeaways include:
- Using
pop
to remove and return a column as a Series. - Handling errors when trying to remove non-existent columns.
- Reusing the returned Series for further operations.
The DataFrame.pop
method is a powerful tool for efficiently removing and extracting columns from a DataFrame.