问题描述
是否有某种方法可以使用Pandas(最好是read_csv)从csv文件中读取具有特定索引的特定列?我知道read_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.
推荐答案
下面是一个示例,说明了EdChum给出的答案.有很多其他选项可以加载CSV文件,请检查 API参考.
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
这篇关于如何使用Pandas从CSV读取特定的列索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!