4 C
London
Friday, December 20, 2024
HomePandas in PythonDataFrame Functions in PythonPandas: How to Drop Columns Not in List

Pandas: How to Drop Columns Not in List

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to drop columns from a pandas DataFrame that are not in a specific list:

#define columns to keep
keep_cols = ['col1', 'col2', 'col3']

#create new dataframe by dropping columns not in list
new_df = df[df.columns.intersection(keep_cols)]

This particular example will drop any columns from the DataFrame that are not equal to col1, col2, or col3.

The following example shows how to use this syntax in practice.

Example: Drop Columns Not in List in Pandas

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12],
                   'steals': [4, 4, 10, 12, 8, 5, 5, 2]})

#view DataFrame
print(df)

  team  points  assists  rebounds  steals
0    A      18        5        11       4
1    B      22        7         8       4
2    C      19        7        10      10
3    D      14        9         6      12
4    E      14       12         6       8
5    F      11        9         5       5
6    G      20        9         9       5
7    H      28        4        12       2

Now suppose that we would like to create a new DataFrame that drops all columns that are not in the following list of columns: team, points, and steals.

We can use the following syntax to do so:

#define columns to keep
keep_cols = ['team', 'points', 'steals']

#create new dataframe by dropping columns not in list
new_df = df[df.columns.intersection(keep_cols)]

#view new dataframe
print(new_df)

  team  points  steals
0    A      18       4
1    B      22       4
2    C      19      10
3    D      14      12
4    E      14       8
5    F      11       5
6    G      20       5
7    H      28       2

Notice that each of the columns from the original DataFrame that are not in the keep_cols list have been dropped from the new DataFrame.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Drop First Row in Pandas
How to Drop First Column in Pandas
How to Drop Duplicate Columns in Pandas
How to Drop All Columns Except Specific Ones in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories