Table of Content
- Introduction
- Syntax and Parameters
- How to use .drop() func?
- Error Handling
- How to use .drop() based on conditions
- Common Mistakes and Fixes
- Sorting
- Handling Missing Data
- Aggregations & Statistics
- Grouping & Pivoting
- String Methods (
.str) - Datetime Handling (
.dt) - Reshaping
- Merging & Joining
- Apply & Lambda
1. Introduction
When working with DataFrames in Pandas, you often need to remove unnecessary rows or columns — maybe a feature you don’t need, a column of missing data, or an incorrect row.
Python gives you a simple yet powerful function for this: drop()
⭐ What is drop() Function?
The
.drop() function is used to remove rows or columns from a DataFrame.By using axis='columns' or columns=, the drop() function deletes the column you mention.
By using axis='index' or index=, the drop() function deletes the row you mention.
2. Syntax and Parameters
These parameters — labels, axis, index, columns, level, inplace, and errors — are provided as Keyword Arguments.
| Parameter | Description |
| labels | Row index or column name(s) to drop (depends on axis). |
| axis | Choose what to drop: 0 = rows, 1 = columns. |
| index | Directly specify row index(es) to remove. |
| columns | Directly specify column name(s) to remove. |
| level | Used for MultiIndex; defines which index level to target. |
| inplace | True:change original DataFrame. False:return new one. |
| errors | 'raise': show error if label not found. 'ignore': skip missing labels. |
3. How to use .drop() function?
⚡️ How to Drop a Column from a DataFrame?
Code Explanation:
df.drop() - tell pandas to delete the column named Marks.
df_new - stores the updated DataFrame without that column.
⛳ Output:
⚡️ How to Drop multiple Columns from a DataFrame?
Code Explanation:
df.drop() - tell pandas to delete the columns named Age and Marks
df_new - stores the updated DataFrame without that column.
⚡️ How to Drop a Row from a DataFrame?
Code Explanation:
df.drop() - tell pandas to delete the row with index=1
df_new - stores the updated DataFrame without that row.
⚡️ How to Drop multiple Rows from a DataFrame?
Code Explanation:
df.drop() - tell pandas to delete the row with index 1 and 3
df_new - stores the updated DataFrame without that row.
4. Error Handling
Sometimes you may try to drop a row or column that does not exist in the DataFrame.
By default, Pandas gives an error - KeyError: "['SomeLabel'] not found in axis"
To avoid the error, you can use: errors='ignore'
This tells Pandas:
👉 “If the label is missing, don’t stop the program — just continue.”
Code Explanation:
- The DataFrame has only Name, Age and Marks columns.
- We attempt to drop “City”, which does not exist.
- Instead of giving an error, errors='ignore' allows the code to run smoothly.
- Since the column is not found, the DataFrame stays the same.
5. How to drop rows and columns based on Conditions?
- Drop rows where Age < 21
- Drop rows where Marks < 40
- Drop rows where Name is missing
- Drop rows containing outliers
Pandas allows you to do this easily using Boolean Filtering (True/False condition) - a method which keeps only the rows where the condition is True and removes the rows where the condition is False.
Example: Drop Rows Where Marks < 90
Code Explanation:
- df['Marks'] >= 90 creates a condition
- Only rows where this condition is True are kept
- Rows where Marks < 90 (Amit and Sam) are removed
⭐ Drop Rows Using Multiple Conditions in Pandas
You can combine multiple conditions using:
& → AND
| → OR
~ → NOT
Example: Keep rows where Age > 20 AND Marks > 85
Code Explanation:
- (df['Age'] > 20) — Condition 1: Age > 20 → True for Amit and Riya
(df['Marks'] > 85) — Condition 2: Marks > 85 → True for Riya and Sam
Using
&, only rows where both conditions are True are kept - Only Riya satisfies both
6. Common Mistakes and Fixes
1️⃣ Dropping non-existing column → KeyError
❌ Mistake:df.drop(columns=['City'])
✅ Fix:df.drop(columns=['City'], errors='ignore')
2️⃣ Wrong axis when dropping columns
df.drop('Marks')
✅ Fix:df.drop(columns=['Marks']) or df.drop('Marks', axis="1)
3️⃣ Missing parentheses in multiple conditions
❌ Mistake:df[df['Age'] > 20 & df['Marks'] > 85]
✅ Fix:df[(df['Age'] > 20) & (df['Marks'] > 85)]
4️⃣ Using and / or instead of & / |
❌ Mistake:df[(df['Age'] > 20) and (df['Marks'] > 85)]
✅ Fix:df[(df['Age'] > 20) & (df['Marks'] > 85)]
5️⃣ Expecting drop() to modify original DataFrame
❌ Mistake:df[(df['Age'] > 20) and (df['Marks'] > 85)]
✅ Fix:df[(df['Age'] > 20) & (df['Marks'] > 85)]
Summary
- import pandas as pd
- pd.__version__ → check version
- pd.set_option('display.max_rows', n) → control display
axis='columns' or columns=... → Removes columns
axis='index' or index=... → Removes rows
- pd.read_csv('file.csv')
- pd.read_excel('file.xlsx')
- pd.read_json('file.json')
- pd.read_sql(query, connection)
- df.to_csv('file.csv')
- df.to_excel('file.xlsx')
