Drop Rows and Columns
drop removes labels. Use axis=1 or columns= for columns. It returns a new DataFrame by default.
Example
import pandas as pd
import numpy as np
df = pd.DataFrame({'name': ['Ada', 'Bob', 'Cy', 'Di'], 'age': [30, 25, 35, 28], 'city': ['Lagos', 'Accra', 'Lagos', 'Nairobi'], 'score': [88.5, 92.0, 79.5, 85.0]})
print(df.drop(columns=['city', 'score']))
print(df.drop(index=[0, 1]))
print(df.shape)Result
name age 0 Ada 30 1 Bob 25 2 Cy 35 3 Di 28 name age city score 2 Cy 35 Lagos 79.5 3 Di 28 Nairobi 85.0 (4, 4)
Exercise
Drop a column.
df.drop(=["age"])
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.