Skip to content Skip to sidebar Skip to footer

How To Read Specific Column Index From Csv Using Pandas

Is there some way of reading only a particular column with specific index from a csv file using Pandas(preferably read_csv)? I understand that read_csv provides the ability to read

Solution 1:

Here is an example illustrating the answer given by EdChum. There is a lot of additional options to load a CSV file, check the API reference.

raw_data = {'first_name': ['Steve', 'Guido', 'John'],
        'last_name': ['Jobs', 'Van Rossum', "von Neumann"]}
df = pd.DataFrame(raw_data)
# Saving data without header
df.to_csv(path_or_buf='test.csv', header=False)
# Telling that there is no header and loading only the first name
df = pd.read_csv(filepath_or_buffer='test.csv', header=None, usecols=[1], names=['first_name'])
df

  first_name
0      Steve
1      Guido
2       John

Solution 2:

import pandas as pd
data = pd.read_csv('file.csv', usecols=['column_name'])

Parameter of usecols contain list of column name(s). If want more than one columns,then separate them by comma i.e., ['column_name1, 'column_name2', 'column_name3']


Post a Comment for "How To Read Specific Column Index From Csv Using Pandas"