Examples of Dropping Columns
Let’s go through some examples to understand how to use the drop() method effectively in python.
Dropping a Single Column
Suppose you have the following DataFrame:
Python
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
})
print("Original DataFrame:")
print(df)

You can also try this code with Online Python Compiler
Run Code
Output
Name Age City
0 Alice 25 New York
1 Bob 30 Los Angeles
2 Charlie 35 Chicago
To drop the Age column
Python
df_dropped = df.drop('Age', axis=1)
print("\nDataFrame after dropping 'Age' column:")
print(df_dropped)

You can also try this code with Online Python Compiler
Run Code
Output
Name City
0 Alice New York
1 Bob Los Angeles
2 Charlie Chicago
Dropping Multiple Columns
If you want to drop multiple columns, pass a list of column names:
Python
df_dropped = df.drop(['Age', 'City'], axis=1)
print("\nDataFrame after dropping 'Age' and 'City' columns:")
print(df_dropped)

You can also try this code with Online Python Compiler
Run Code
Output
Name
0 Alice
1 Bob
2 Charlie
Dropping Columns In-Place
If you want to modify the original DataFrame, set inplace=True:
Python
df.drop('City', axis=1, inplace=True)
print("\nOriginal DataFrame after dropping 'City' column (in-place):")
print(df)

You can also try this code with Online Python Compiler
Run Code
Output
Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
Key Features of Dropping Columns
- Flexibility: You can drop one or multiple columns at once.
- In-Place Modification: Allows you to modify the DataFrame directly or return a new one.
- Handling Missing Data: Easily remove columns with missing values.
Handling Missing Data
Sometimes, you may need to drop columns that contain missing values. You can use the dropna() method for this purpose:
Python
df = pd.DataFrame({
'A': [1, 2, None],
'B': [4, None, 6],
'C': [7, 8, 9]
})
print("Original DataFrame with missing values:")
print(df)
df_cleaned = df.dropna(axis=1)
print("\nDataFrame after dropping columns with missing values:")
print(df_cleaned)

You can also try this code with Online Python Compiler
Run Code
Output
Original DataFrame with missing values:
A B C
0 1.0 4.0 7
1 2.0 NaN 8
2 NaN 6.0 9
DataFrame after dropping columns with missing values:
C
0 7
1 8
2 9
Advanced Techniques
Dropping Columns Based on Condition
You can drop columns based on their names using list comprehensions:
Python
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'Unwanted_Column': [7, 8, 9]
})
# Drop columns that contain 'Unwanted' in their name
cols_to_drop = [col for col in df.columns if 'Unwanted' in col]
df_dropped = df.drop(cols_to_drop, axis=1)
print("\nDataFrame after conditionally dropping columns:")
print(df_dropped)

You can also try this code with Online Python Compiler
Run Code
Output
A B
0 1 4
1 2 5
2 3 6
Using filter() Method
You can also use the filter() method to select columns and exclude the rest:
Python
df_filtered = df.filter(regex='^(?!Unwanted)')
print("\nDataFrame after filtering columns:")
print(df_filtered)

You can also try this code with Online Python Compiler
Run Code
Output
A B
0 1 4
1 2 5
2 3 6
Frequently Asked Questions
How do I drop columns from a DataFrame in Pandas?
To remove columns from a DataFrame, use the drop() method. Set the axis parameter to 1 to specify that you want to drop columns. For instance, df.drop('column_name', axis=1) removes the column named 'column_name'. To drop multiple columns, pass a list of names, like df.drop(['col1', 'col2'], axis=1).
Can I drop columns with missing values?
Yes, you can drop columns with missing values using the dropna() method. Set the axis parameter to 1 to drop columns. For example, df.dropna(axis=1) will remove any columns containing at least one NaN value. This is useful for cleaning data and removing incomplete columns.
How do I make the change directly in the existing DataFrame?
To modify the original DataFrame directly without creating a new one, use the inplace parameter with the drop() method. For example, df.drop('column_name', axis=1, inplace=True) will remove the column and update the DataFrame in place, reflecting the changes immediately without returning a new DataFrame.
Conclusion
Dropping columns in Pandas is a pretty easy yet essential operation for data manipulation. By mastering this technique, you can clean your data, focus on important features, and enhance your data analysis workflows. Whether you're cleaning up data or preparing it for analysis, Pandas provides the tools you need to work on efficiently.
You can also check out our other blogs on Code360.