How can I drop all columns in a Pandas dataframe except for specific ones?

How can I drop all columns in a Pandas dataframe except for specific ones?

The process of dropping all columns in a Pandas dataframe except for specific ones involves using the “drop” function with the “columns” parameter. This allows for selecting and dropping specific columns from the dataframe while retaining the desired ones. By specifying the columns to be dropped, the dataframe can be effectively manipulated to only include the desired columns. This method is useful for data cleaning and organizing tasks, where only certain columns are relevant for analysis or further processing.

Pandas: Drop All Columns Except Specific Ones


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.

Related:

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

Cite this article

stats writer (2024). How can I drop all columns in a Pandas dataframe except for specific ones?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-drop-all-columns-in-a-pandas-dataframe-except-for-specific-ones/

stats writer. "How can I drop all columns in a Pandas dataframe except for specific ones?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-drop-all-columns-in-a-pandas-dataframe-except-for-specific-ones/.

stats writer. "How can I drop all columns in a Pandas dataframe except for specific ones?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-drop-all-columns-in-a-pandas-dataframe-except-for-specific-ones/.

stats writer (2024) 'How can I drop all columns in a Pandas dataframe except for specific ones?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-drop-all-columns-in-a-pandas-dataframe-except-for-specific-ones/.

[1] stats writer, "How can I drop all columns in a Pandas dataframe except for specific ones?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I drop all columns in a Pandas dataframe except for specific ones?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)

Comments are closed.

Slide Up
x
PDF
Scroll to Top