How to 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

Note that the column located in the last value in the range (3) will not be included in the output.

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

Additional Resources

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

How to List All Column Names in Pandas
How to Drop Columns in Pandas
How to Convert Index to Column in Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *