How can I exclude columns in Pandas?

How can I exclude columns in Pandas?

Pandas is a popular Python library used for data manipulation and analysis. It offers various functions to filter and select specific columns from a dataset. However, there may be instances where we need to exclude certain columns from our analysis. To achieve this, Pandas provides the “drop” function which allows us to specify the columns we want to exclude from our dataset. This function can be used with different parameters such as column names or indexes to exclude specific columns. Additionally, we can also use the “drop” function to exclude multiple columns at once. By utilizing this functionality, we can easily exclude columns in Pandas and perform our analysis on the desired subset of data.

Exclude Columns in Pandas (With Examples)


You can use the following syntax to exclude columns in a pandas DataFrame:

#exclude column1
df.loc[:, df.columns!='column1']

#exclude column1, column2, ...
df.loc[:, ~df.columns.isin(['column1', 'column2', ...])]

The following examples show how to use this syntax in practice.

Example 1: Exclude One Column

The following code shows how to select all columns except one in a pandas DataFrame:

import pandas as pd

#create DataFrame 
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12],
                   'blocks': [2, 3, 3, 5, 3, 2, 1, 2]})

#view DataFrame
df

	points	assists	rebounds blocks
0	25	5	11	 2
1	12	7	8	 3
2	15	7	10	 3
3	14	9	6	 5
4	19	12	6	 3
5	23	9	5	 2
6	25	9	9	 1
7	29	4	12	 2

#select all columns except 'rebounds'
df.loc[:, df.columns!='rebounds']

        points	assists	blocks
0	25	5	2
1	12	7	3
2	15	7	3
3	14	9	5
4	19	12	3
5	23	9	2
6	25	9	1
7	29	4	2

Example 2: Exclude Multiple Columns

The following code shows how to select all columns except specific ones in a pandas DataFrame:

import pandas as pd

#create DataFrame 
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12],
                   'blocks': [2, 3, 3, 5, 3, 2, 1, 2]})

#view DataFrame
df

	points	assists	rebounds blocks
0	25	5	11	 2
1	12	7	8	 3
2	15	7	10	 3
3	14	9	6	 5
4	19	12	6	 3
5	23	9	5	 2
6	25	9	9	 1
7	29	4	12	 2

#select all columns except 'rebounds' and 'assists'
df.loc[:, ~df.columns.isin(['rebounds', 'assists'])]

	points	blocks
0	25	2
1	12	3
2	15	3
3	14	5
4	19	3
5	23	2
6	25	1
7	29	2

Using this syntax, you can exclude any number of columns that you’d like by name.

Cite this article

stats writer (2024). How can I exclude columns in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-exclude-columns-in-pandas/

stats writer. "How can I exclude columns in Pandas?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-exclude-columns-in-pandas/.

stats writer. "How can I exclude columns in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-exclude-columns-in-pandas/.

stats writer (2024) 'How can I exclude columns in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-exclude-columns-in-pandas/.

[1] stats writer, "How can I exclude columns in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I exclude columns in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top