5.3 C
London
Thursday, December 19, 2024
HomePandas in PythonInput/Output in PythonPandas: How to Use read_csv with usecols Argument

Pandas: How to Use read_csv with usecols Argument

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories