pandas 如何使用pandas从csv读取特定的列索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32441633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read specific column index from csv using pandas
提问by W.Ali
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.
是否有某种方法可以使用 Pandas(最好是 read_csv)从 csv 文件中仅读取具有特定索引的特定列?我知道 read_csv 提供了按列名读取特定列的能力,但数据文件没有标题,所以我不能使用列名。请注意,文件太大,因此我不想读取整个文件然后再读取子集。谢谢。
回答by Romain
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.
这是一个示例,说明了 EdChum 给出的答案。加载 CSV 文件有很多附加选项,请查看API 参考。
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
回答by Irfanullah
import pandas as pd
data = pd.read_csv('file.csv', usecols=['column_name'])
Parameter of usecolscontain list of column name(s). If want more than one columns,then separate them by comma i.e., ['column_name1, 'column_name2', 'column_name3']
usecols包含列名列表的参数。如果想要多于一列,然后用逗号分隔它们,即,['column_name1, 'column_name2', 'column_name3']

