为列表中的每个数据框添加一列时出现问题

Problem adding a column for each dataframe in a list

Python, 蜘蛛

你好

我有一个包含 1440 个文件的文件夹,每个文件代表一天的时间戳,文件名具有该时间戳。在下面的代码中,我制作了所有这些文件的数据框列表。

对于每个数据框,我需要一个包含文件名的列。

使用以下代码,我得到错误 "AttributeError: 'DataFrame' object has no attribute 'all_filenames'"

我做错了什么?

import glob
import os
import pandas as pd
import nympy as np

os.chdir("I:/INRIX and BeMobile/BeMobile/2017-03-13")
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]

tempList = []

runUpTo = 30

for i in range(len(all_filenames[:runUpTo])):
    print('Currently in iteration ' + str(i) + ' of ' + str(len(all_filenames)))

    temp = pd.read_csv(all_filenames[i], sep=';', skiprows=1, header=None)
    temp.columns = ['Delete1','segmentID','Duration','Delete2',]
    temp = temp[['segmentID','Duration']]

    temp = temp.sort_values('segmentID')
    temp.index = np.arange(len(temp))

    tempList.append(temp)

#add column with time stamp
#%%

for i in range(len(tempList[:runUpTo])):
    tempList[i].is_copy = False
    tempList[i]['Timestamp'] = tempList[i].all_filenames[i]

您实际上还没有将名为 "all_filenames" 的列添加到您的数据框中。

在您的代码中的某处,您需要执行以下操作:

temp['all_filenames'] = 'TheActualFileName'

然后您可以通过以下方式访问它:

tempList[i]['all_filenames']