Table of Contents
The usecols argument in the read_csv function of Pandas allows users to specify which columns they want to import from a CSV file. This can be helpful when working with large datasets that have many columns, as it allows users to select only the relevant columns for their analysis. The argument takes in a list of column names or indices, and only those columns will be imported into the DataFrame. This feature helps to improve the efficiency and speed of data processing, as it eliminates the need to load unnecessary columns. Overall, the usecols argument provides a convenient way for users to customize their data import process in Pandas.
Pandas: Use read_csv with usecols Argument
You can use the usecols argument within the read_csv() function to read specific columns from a CSV file into a pandas DataFrame.
There are two common ways to use this argument:
Method 1: Use usecols with Column Names
df = pd.read_csv('my_data.csv', usecols=['this_column', 'that_column'])
Method 2: Use usecols with Column Positions
df = pd.read_csv('my_data.csv', usecols=[0, 2])
The following examples show how to use each method in practice with the following CSV file called basketball_data.csv:

Example 1: Use usecols with Column Names
We can use the following code to import the CSV file and only use the columns called ‘team’ and ‘rebounds’:
import pandas as pd #import DataFrame and only use 'team' and 'rebounds' columns df = pd.read_csv('basketball_data.csv', usecols=['team', 'rebounds']) #view DataFrame print(df) team rebounds 0 A 10 1 B 9 2 C 6 3 D 2
Notice that only the team and rebounds columns were imported since these were the names of the columns that we specified in the usecols argument.
Example 2: Use usecols with Column Positions
We can use the following code to import the CSV file and only use the columns in index positions 0 and 2:
import pandas as pd #import DataFrame and only use columns in index positions 0 and 2 df = pd.read_csv('basketball_data.csv', usecols=[0, 2]) #view DataFrame print(df) team rebounds 0 A 10 1 B 9 2 C 6 3 D 2
Notice that only the team and rebounds columns were imported since these were the columns in index positions 0 and 2, which are the values that we specified in the usecols argument.
Note: The first column in the CSV file has an index position of 0.
The following tutorials explain how to perform other common tasks in Python:
Cite this article
stats writer (2024). How can the usecols argument be used in the read_csv function in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-usecols-argument-be-used-in-the-read_csv-function-in-pandas/
stats writer. "How can the usecols argument be used in the read_csv function in Pandas?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-usecols-argument-be-used-in-the-read_csv-function-in-pandas/.
stats writer. "How can the usecols argument be used in the read_csv function in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-usecols-argument-be-used-in-the-read_csv-function-in-pandas/.
stats writer (2024) 'How can the usecols argument be used in the read_csv function in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-usecols-argument-be-used-in-the-read_csv-function-in-pandas/.
[1] stats writer, "How can the usecols argument be used in the read_csv function in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the usecols argument be used in the read_csv function in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
