How to read specific column index from csv using pandas

天大地大妈咪最大 提交于 2019-12-12 11:26:48

问题


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.


回答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



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



来源:https://stackoverflow.com/questions/32441633/how-to-read-specific-column-index-from-csv-using-pandas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!