0

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]
nielsen
  • 383
  • 1
  • 6

1 Answers1

1

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']
Diego
  • 34,802
  • 21
  • 91
  • 134
  • The "all_filenames" is there as a list, where each entry has the name of the file which includes the time stamp. The column I want to add is called 'Timestamp'. My question to you is. When you say 'TheActualFileName', do you mean the "all_filenames" list? – nielsen Mar 31 '20 at 08:19
  • 1
    in that case it would be something like: all_filenames[i] – Diego Mar 31 '20 at 09:04