Search code examples
pythonpandaslistdataframecalculated-columns

Problem adding a column for each dataframe in a list


Python, Spyder

Hello

I have a folder with 1440 files that each represent a time stamp of a day and the filename has that timestamp. In the following code, i have made a dataframe list of all these files.

For each dataframe I need a column with the filename.

With the following code, I get the error "AttributeError: 'DataFrame' object has no attribute 'all_filenames'"

What am I doing wrong?

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]

Solution

  • you havent actually added a column called "all_filenames" to your dataframe.

    Somewhere in your code, you need to do:

    temp['all_filenames'] = 'TheActualFileName'
    

    then you can access it with:

    tempList[i]['all_filenames']