2.4 C
London
Friday, December 20, 2024
HomePandas in PythonDataFrame Functions in PythonPandas: How to Drop All Columns Except Specific Ones

Pandas: How to Drop All Columns Except Specific Ones

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 methods to drop all columns except specific ones from a pandas DataFrame:

Method 1: Use Double Brackets

df = df[['col2', 'col6']]

Method 2: Use .loc

df = df.loc[:, ['col2', 'col6']]

Both methods drop all columns in the DataFrame except the columns called col2 and col6.

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame with six columns
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, 3, 3, 2, 5, 4, 3, 8],
                   'blocks': [1, 0, 0, 3, 2, 2, 1, 5]})

#view DataFrame
print(df)

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

Example 1: Drop All Columns Except Specific Ones Using Double Brackets

We can use the following syntax to drop all columns in the DataFrame except the ones called points and blocks:

#drop all columns except points and blocks
df = df[['points', 'blocks']]

#view updated DataFrame
print(df)

   points  blocks
0      18       1
1      22       0
2      19       0
3      14       3
4      14       2
5      11       2
6      20       1
7      28       5

Notice that only the points and blocks columns remain.

All other columns have been dropped.

Example 2: Drop All Columns Except Specific Ones Using .loc

We can also use the .loc function to drop all columns in the DataFrame except the ones called points and blocks:

#drop all columns except points and blocks
df = df.loc[:, ['points', 'blocks']]

#view updated DataFrame
print(df)

   points  blocks
0      18       1
1      22       0
2      19       0
3      14       3
4      14       2
5      11       2
6      20       1
7      28       5

Notice that only the points and blocks columns remain.

This matches the results from the previous example.

Related: Pandas loc vs. iloc: What’s the Difference?

Additional Resources

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

How to Drop First Row in Pandas DataFrame
How to Drop First Column in Pandas DataFrame
How to Drop Duplicate Columns 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