如何使用pandas从csv中读取特定的列索引

时间:2015-09-07 15:04:28

标签: python csv pandas

是否有某种方法只使用Pandas(最好是read_csv)从csv文件中读取具有特定索引的特定列?我知道read_csv提供了按列名读取特定列的功能,但数据文件没有标题,因此我不能使用列名。请注意,该文件太大,所以我不想读取整个文件然后子集。谢谢。

2 个答案:

答案 0 :(得分:5)

这是一个说明EdChum给出的答案的例子。加载CSV文件还有很多其他选项,请查看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

答案 1 :(得分:1)

NFFT=size(Acc_TD_Segments{1},1); % NFFT (lenght of segment) and Acc_TD_Segments(Time domain signal)

Fss=30;                          % the sampling frequency of the input signal 

Acc_FD_Signal{nn,1}=fft(Acc_TD_Segments{nn},NFFT)/NFFT; % nn (number of segments which is =10) 

import pandas as pd data = pd.read_csv('file.csv', usecols=['column_name']) 的参数包含列名列表。如果需要多个列,请用逗号分隔,即usecols