关键错误:' 3'从Pandas DataFrame中提取数据时

时间:2016-09-01 10:09:55

标签: pandas dataframe

我的代码计划如下:

1)使用glob查找文件夹中的csv文件并创建文件列表

2)将每个csv文件转换为数据帧

3)从列位置提取数据并转换为单独的数据帧

4)将新数据附加到单独的摘要csv文件

代码如下:

Result = []

def result(filepath):
    files = glob.glob(filepath)
    print files
    dataframes = [pd.DataFrame.from_csv(f, index_col=None) for f in files]
    new_dfb = pd.DataFrame()

    for i, df in enumerate(dataframes):
        colname = 'Run {}'.format(i+1)
        selected_data = df['3'].ix[0:4] 
        new_dfb[colname] = selected_data
    Result.append(new_dfb)
    folder = r"C:/Users/Joey/Desktop/tcd/summary.csv"
    new_dfb.to_csv(folder)

result("C:/Users/Joey/Desktop/tcd/*.csv")

print Result

代码错误如下所示。这个问题似乎与第36行相对应,它与selected_data = df['3'].ix[0:4]相对应。

enter image description here

我在下面展示了我的一个csv文件:

enter image description here

我不确定数据帧构造函数的问题是什么?

1 个答案:

答案 0 :(得分:1)

你的csv片段有点不清楚。但正如评论中所建议的那样,read_csv(在本例中为from_csv)会自动将第一行作为标题列表。您想要的行为是标记为1,2,3等的列。为此,您需要

  [pd.DataFrame.from_csv(f, index_col=None,header=None) for f in files]