Let us see how to read specific columns of a CSV file using Pandas. This can be done with the help of the pandas.read_csv() method. We will pass the first parameter as the CSV file and the second parameter the list of specific columns in the keyword usecols. It will return the data of the CSV file of specific columns.
import pandas as pd
fields = ['star_name', 'ra']
df = pd.read_csv('data.csv', skipinitialspace=True, usecols=fields)
# See the keys
print df.keys()
# See content in 'star_name'
print df.star_name
The problem here was the skipinitialspace which remove the spaces in the header. So ' star_name' becomes 'star_name'
To read a CSV file, call pd.read_csv(file_name, usecols=cols_list) with file_name as the name of the CSV file, delimiter as the delimiter, and cols_list as the list of specific columns to read from the CSV file. Call df[col] with df as the DataFrame from the previous step, and col as the column name to read.
SAMPLE_FILE.CSV
,Name,Department
0,John,IT
1,Jane,HR
2,Jack,Accounting
col_list = ["Name", "Department"]
df = pd.read_csv("sample_file.csv", usecols=col_list)
print(df["Name"])
OUTPUT
0 John
1 Jane
2 Jack
Name: Name, dtype: object
print(df["Department"])
OUTPUT
0 IT
1 HR
2 Accounting
Name: Department, dtype: object
pandas.read_csv(filepath_or_buffer, skipinitialspace=True, usecols=columns)
with the name of a .csv
file as filepath_or_buffer
and a list of the desired columns' names as columns