Read a Subset of Columns from a CSV into a Pandas DataFrame

Python

To read only a subset of columns into a Pandas dataframe from a csv file, pass a list of columns to the "usecols" parameter when calling read_csv.

In the example below, only the columns order_number, product and sale_price are read into the dataframe.

 1|  import pandas as pd
 2|  
 3|  columns = ['order_number', 'product', 'sale_price']
 4|  df = pd.read_csv('sales.csv', usecols=columns)
Did you find this snippet useful?

Sign up for free to to add this to your code library