How can I select multiple columns in Pandas?

How can I select multiple columns in Pandas?

Pandas is a popular Python library used for data analysis and manipulation. It provides various functions and methods to efficiently handle and process large datasets. One of the common tasks in data analysis is selecting specific columns from a dataset. In Pandas, this can be done by using the “loc” or “iloc” methods. These methods allow users to select multiple columns by specifying the column names or indexes. Additionally, the “loc” method can also be used to select rows and columns simultaneously. This feature makes Pandas a powerful tool for selecting and manipulating data in a flexible and efficient manner.

Select Multiple Columns in Pandas (With Examples)


There are three basic methods you can use to select multiple columns of a pandas DataFrame:

Method 1: Select Columns by Index

df_new = df.iloc[:, [0,1,3]]

Method 2: Select Columns in Index Range

df_new = df.iloc[:, 0:3]

Method 3: Select Columns by Name

df_new = df[['col1', 'col2']]

The following examples show how to use each method with the following 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': [4, 7, 7, 6, 5, 8, 9, 10]})

#view DataFrame
df

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

Method 1: Select Columns by Index

The following code shows how to select columns in index positions 0, 1, and 3:

#select columns in index positions 0, 1, and 3
df_new = df.iloc[:, [0,1,3]]

#view new DataFrame
df_new

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

Notice that the columns in index positions 0, 1, and 3 are selected.

Note: The first column in a pandas DataFrame is located in position 0.

Method 2: Select Columns in Index Range

The following code shows how to select columns in the index range 0 to 3:

#select columns in index range 0 to 3
df_new = df.iloc[:, 0:3]

#view new DataFrame
df_new

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

Method 3: Select Columns by Name

The following code shows how to select columns by name:

#select columns called 'points' and 'blocks'
df_new = df[['points', 'blocks']]

#view new DataFrame
df_new

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

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

Cite this article

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

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

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

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

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

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

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