Load & Import Particular Columns from CSV File as pandas DataFrame in Python (Example Code)

In this Python tutorial you’ll learn how to import only some specific columns from a CSV file.

Setting up the Example

import pandas as pd                                          # Import pandas library
my_df = pd.DataFrame({'A':range(100, 105),                   # Construct new pandas DataFrame
                      'B':[9, 3, 7, 1, 2],
                      'C':['a', 'f', 'c', 'f', 'f'],
                      'D':range(35, 30, - 1)})
my_df.to_csv('my_df.csv')                                    # Write exemplifying DataFrame to folder

Example: Read pandas DataFrame from CSV File & Ignore Unnamed Index Column

my_df_load = pd.read_csv('my_df.csv', usecols = ['A', 'C'])  # Import CSV & print output
print(my_df_load)
#      A  C
# 0  100  a
# 1  101  f
# 2  102  c
# 3  103  f
# 4  104  f

Related Articles

Please find some related Python programming tutorials below:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top