› Forums › Python › Pandas (Python library) › Tutorial: How to Drop Multiple Columns in a Pandas DataFrame
- This topic is empty.
-
AuthorPosts
-
February 14, 2026 at 1:01 pm #6069
π― Learning Objective
In this tutorial, you will learn:
β How to remove more than one column in Pandas
β Howinplace=Trueworks
β How to avoid errors when columns are missing
β Best practices for real-world datasets
π Step 1: Understanding the Context
In real-world datasets, we often create temporary columns during data cleaning.
Example:
"lat-lon"β Used for splitting latitude and longitude"place_with_parent_names"β Used for location parsing
After processing, these columns are no longer needed.
Keeping them:
β Increases dataset size
β Makes analysis confusing
β Slows down ML pipelinesSo, we remove them.
π Step 2: Sample Dataset
Letβs start with a simple example:
import pandas as pd df1 = pd.DataFrame({ "id": [1, 2, 3], "lat-lon": ["19.07,72.87", "28.70,77.10", "13.08,80.27"], "place_with_parent_names": ["Mumbai, India", "Delhi, India", "Chennai, India"], "price_usd": [120000, 150000, 90000] }) print(df1)Output:
id lat-lon place_with_parent_names price_usd 1 19.07,72.87 Mumbai, India 120000 2 28.70,77.10 Delhi, India 150000 3 13.08,80.27 Chennai, India 90000
π Step 3: Dropping Multiple Columns
To remove more than one column, use a list:
df1.drop( columns=["lat-lon", "place_with_parent_names"], inplace=True )
π Step 4: How This Code Works
1οΈβ£
columns=[...]This tells Pandas which columns to remove:
["lat-lon", "place_with_parent_names"]You can add more names if needed.
2οΈβ£
inplace=TrueThis modifies the original DataFrame.
Without it:
df1 = df1.drop(columns=[...])With it:
df1.drop(columns=[...], inplace=True)No reassignment needed.
π Step 5: Result After Dropping
print(df1)Output:
id price_usd 1 120000 2 150000 3 90000 Now the dataset is clean and focused.
π Step 6: Avoiding Errors (Best Practice)
Sometimes columns may be missing.
This causes:
KeyErrorβ Safe Version
df1.drop( columns=["lat-lon", "place_with_parent_names"], inplace=True, errors="ignore" )This skips missing columns and prevents crashes.
π Step 7: Using a Variable (Professional Style)
For large projects, use a variable:
cols_to_drop = [ "lat-lon", "place_with_parent_names" ] df1.drop(columns=cols_to_drop, inplace=True, errors="ignore")β Easier to update
β Cleaner code
β Better readability
π Step 8: Verify Your Changes
Always confirm:
print(df1.columns)Output:
Index(['id', 'price_usd'], dtype='object')
β Step 9: Real-World Use Case
In data science projects, this method is used after:
β Feature extraction
β Data cleaning
β Location processing
β Currency conversion
β Text parsingExample pipeline:
Raw Data β Clean β Extract β Drop β Analyze β ModelDropping unused columns is a key step.
β Summary
Task Code Drop multiple columns df.drop(columns=[...])Modify original inplace=TrueAvoid errors errors="ignore"Best practice Use a variable
π Final Recommended Code
cols_to_drop = ["lat-lon", "place_with_parent_names"] df1.drop( columns=cols_to_drop, inplace=True, errors="ignore" )
-
AuthorPosts
- You must be logged in to reply to this topic.

