Pandas: How to Read Specific Columns from Excel File


You can use the following methods to read specific columns from an Excel file into a pandas DataFrame:

Method 1: Read Specific Columns

df = pd.read_excel('my_data.xlsx', usecols='A, C')

Method 2: Read a Range of Columns

df = pd.read_excel('my_data.xlsx', usecols='A:C') 

Method 3: Read Multiple Ranges of Columns

df = pd.read_excel('my_data.xlsx', usecols='A:C, F, G:J') 

The following examples show how to use each method in practice with the following Excel file called player_data.xlsx:

Example 1: Read Specific Columns

We can use the following code to import the data in columns A and C from the Excel file:

import pandas as pd

#import columns A and C from Excel file
df = pd.read_excel('player_data.xlsx', usecols='A, C')

#view DataFrame
print(df)

  team  rebounds
0    A         8
1    B        12
2    C         4
3    D         4
4    E         6
5    F         7

Notice that only the data from columns A and C in the Excel file were imported.

Example 2: Read a Range of Columns

We can use the following code to import the data in columns A through C from the Excel file:

import pandas as pd

#import columns A through C from Excel file
df = pd.read_excel('player_data.xlsx', usecols='A:C')

#view DataFrame
print(df)

  team  points  rebounds
0    A      24         8
1    B      20        12
2    C      15         4
3    D      19         4
4    E      32         6
5    F      13         7

Notice that only the data from columns A through C in the Excel file were imported.

Example 3: Read Multiple Ranges of Columns

We can use the following code to import the data in columns A through C and column D from the Excel file:

import pandas as pd

#import columns A through C from Excel file
df = pd.read_excel('player_data.xlsx', usecols='A:C, D')

#view DataFrame
print(df)

  team  points  rebounds  assists
0    A      24         8        5
1    B      20        12        3
2    C      15         4        7
3    D      19         4        8
4    E      32         6        8
5    F      13         7        9

Notice that the data from columns A through C and column D in the Excel file were imported.

Note: You can find the complete documentation for the pandas read_excel() function here.

Additional Resources

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

Pandas: How to Skip Rows when Reading Excel File
Pandas: How to Specify dtypes when Importing Excel File
Pandas: How to Combine Multiple Excel Sheets

Featured Posts

Leave a Reply

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