Sometimes you may want to read only specific columns from a CSV file.
You can read specific columns from a CSV using read_csv(‘addresses.csv’, usecols=[1,2]) in Pandas.
Syntax
import pandas as pd
df = pd.read_csv(‘filename.csv', usecols=[1,2])
df
This tutorial teaches you how to read specific columns from a CSV file using the read_CSV()
method and handle headers while reading specific columns.
Reading Specific Columns Using Read_CSV and UserCols
The read_csv()
method reads the CSV file and returns a dataframe.
To read only specific columns,
- Use the
usecols
parameter and pass the column indexes as a list to the usecols parameter. Alternatively, you can also pass the column names as a list. - Indexes are 0-based. To read the first column, use index
0
.
Code
The following code demonstrates how to read the specific columns at index 1
and 2
from the CSV file.
import pandas as pd
df = pd.read_csv('addresses.csv', usecols=[1,2])
df
DataFrame Will Look Like
The first two columns of the CSV file are read, and the dataframe is created.
1 | 2 | |
---|---|---|
0 | Last Name | Address: |
1 | Doe | 120 jefferson st. |
2 | Aruchamy | Chepauk, second street |
Read Specific Columns and Set Column Names
You can use the names
parameter to set column names while reading certain columns from the CSV file.
- Pass the specific column names to read as a list using the
usecols
parameter. - Use the
names
parameter to pass the list of column names - Use the
header=None
to ignore the headers available in the CSV file.
Code
The following code demonstrates how to read the first two columns and set specific column names for those columns.
import pandas as pd
df = pd.read_csv('addresses.csv', header=None, usecols=[1,2], names=['Last Name’, ‘Address’])
df
DataFrame Will Look Like
First Name | Last Name | |
---|---|---|
0 | Last Name | Address: |
1 | Doe | 120 jefferson st. |
2 | Aruchamy | Chepauk, second street |
Read Specific Columns With Headers
To read specific columns with the header, use the header parameter in the read_csv()
file.
- Use the
header=0
parameter to consider the first row as the header - Use the
usecols
parameter and pass the list of columns you need to read.
The specific columns will be read, and the headers available in the CSV file for those columns will also be used as headers in the dataframe.
Code
The following code demonstrates how to read the columns at the index 1
and 2
and use the header available in the CSV file.
import pandas as pd
df = pd.read_csv('addresses.csv', header=0, usecols=[1,2])
df
DataFrame Will Look Like
First Name | Last Name | |
---|---|---|
0 | Doe | 120 jefferson st. |
1 | Aruchamy | Chepauk, second street |