Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read specific column index from csv using pandas

Tags:

python

pandas

csv

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 specific columns by column names, but the data file has no headers so I cannot use column names. Note that the file is too large, so I do not want to read in the entire file and then subset. Thanks.

like image 823
W.Ali Avatar asked Feb 09 '23 02:02

W.Ali


2 Answers

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
like image 99
Romain Avatar answered Feb 11 '23 16:02

Romain


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']

like image 21
Irfanullah Avatar answered Feb 11 '23 16:02

Irfanullah