How can I select columns by name in Pandas?

How can I select columns by name in Pandas?

Pandas is a popular library in Python used for data manipulation and analysis. One of its key features is the ability to select columns by name from a dataset. This allows users to easily retrieve specific columns of data for further analysis or manipulation. To select columns by name in Pandas, the user can use the “loc” function followed by the column names they wish to select. This function allows for flexible and precise selection of columns, making data analysis more efficient and accurate. Additionally, Pandas also offers various methods for selecting columns based on different criteria, providing users with a wide range of options for data exploration and manipulation.

Select Columns by Name in Pandas (3 Examples)


You can use the following methods to select columns by name in a pandas DataFrame:

Method 1: Select One Column by Name

df.loc[:, 'column1']

Method 2: Select Multiple Columns by Name

df.loc[:, ['column1', 'column3', 'column4']] 

Method 3: Select Columns in Range by Name

df.loc[:, 'column2':'column4'] 

The following examples show how to use each of these methods in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'mavs': [10, 12, 14, 15, 19, 22, 27],
                   'cavs': [18, 22, 19, 14, 14, 11, 20],
                   'hornets': [5, 7, 7, 9, 12, 9, 14],
                   'spurs': [10, 12, 14, 13, 13, 19, 22],
                   'nets': [10, 14, 25, 22, 25, 17, 12]})

#view DataFrame
print(df)

   mavs  cavs  hornets  spurs  nets
0    10    18        5     10    10
1    12    22        7     12    14
2    14    19        7     14    25
3    15    14        9     13    22
4    19    14       12     13    25
5    22    11        9     19    17
6    27    20       14     22    12

Example 1: Select One Column by Name

The following code shows how to select the ‘spurs’ column in the DataFrame:

#select column with name 'spurs'
df.loc[:, 'spurs']

0    10
1    12
2    14
3    13
4    13
5    19
6    22
Name: spurs, dtype: int64

Only the values from the ‘spurs’ column are returned.

Example 2: Select Multiple Columns by Name

The following code shows how to select the cavs, spurs, and nets columns in the DataFrame:

#select columns with names cavs, spurs, and nets
df.loc[:, ['cavs', 'spurs', 'nets']]

        cavs	spurs	nets
0	18	10	10
1	22	12	14
2	19	14	25
3	14	13	22
4	14	13	25
5	11	19	17
6	20	22	12

Only the values from the cavs, spurs, and nets columns are returned.

Example 3: Select Columns in Range by Name

The following code shows how to select all columns between the names ‘hornets’ and ‘nets’ in the DataFrame:

#select all columns between hornets and nets
df.loc[:, 'hornets':'nets']

        hornets	spurs	nets
0	5	10	10
1	7	12	14
2	7	14	25
3	9	13	22
4	12	13	25
5	9	19	17
6	14	22	12

All of the columns between the names ‘hornets’ and ‘nets’ are returned.

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

Cite this article

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

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

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

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

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

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

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