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.
Additional Resources
The following tutorials explain how to perform other common tasks in Python:
Pandas: How to Skip Rows when Reading CSV File
Pandas: How to Read Excel Files
Pandas: How to Export DataFrame to Excel