How to Exclude Columns in Pandas – Python

To exclude columns in a Pandas DataFrame, you can use the drop() method. Below are a few examples of how to exclude columns using drop():

Methods:

1. Exclude Columns by Name:

import pandas as pd

# Sample DataFrame
data = {
    "A": [1, 2, 3],
    "B": [4, 5, 6],
    "C": [7, 8, 9]
}
df = pd.DataFrame(data)

# Exclude column 'B'
df_excluded = df.drop(columns=["B"])
print(df_excluded)

Output:

A C
0 1 7
1 2 8
2 3 9

2. Exclude Columns by Index:

# Exclude the second column (index 1)
df_excluded = df.drop(df.columns[1], axis=1)
print(df_excluded)

Output:

 A C
0 1 7
1 2 8
2 3 9

3. Exclude Multiple Columns:

# Exclude columns 'A' and 'C'
df_excluded = df.drop(columns=["A", "C"])
print(df_excluded)

Output:

 B
0 4
1 5
2 6

4. Using filter() to Keep Desired Columns:

# Keep only columns 'A' and 'C'
df_excluded = df.filter(["A", "C"])
print(df_excluded)

Output:

 A C
0 1 7
1 2 8
2 3 9

5. Exclude Columns with Specific Conditions:

# Exclude columns that start with 'B'
df_excluded = df.loc[:, ~df.columns.str.startswith("B")]
print(df_excluded)

Output:

 A C
0 1 7
1 2 8
2 3 9

Summary:

In Pandas, excluding columns from a DataFrame can be achieved using the versatile drop() method or alternative approaches. The drop() method allows you to remove columns by specifying their names in the columns parameter, e.g., df.drop(columns=[“col1”]). For multiple columns, simply pass a list of column names. If you prefer to drop columns by their index positions, you can use df.drop(df.columns[index], axis=1). Another approach is to select the desired columns and exclude others by using the filter() method, e.g., df.filter([“col1”, “col2”]).

You can also exclude columns based on specific conditions. For example, to remove columns whose names start with a specific pattern, use the loc function with logical indexing, such as df.loc[:, ~df.columns.str.startswith(“pattern”)]. These methods provide flexibility in handling columns dynamically, whether you’re working with names, indices, or conditions. Overall, Pandas makes it straightforward to exclude columns from a DataFrame, helping you customize data for analysis or visualization.

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top