1

I have multiple files that are named like this
2018-08-31-logfile-device1 2018-09-01-logfile-device1

in these files the data is sorted this way:
00:00:00.283672analogue values:[2511, 2383, 2461, 2472] 00:00:00.546165analogue values:[2501, 2395, 2467, 2465]

I append all these files into one big dataframe with this code: (i got from here: Import multiple excel files into python pandas and concatenate them into one dataframe)

file_log = os.listdir(path)
file_log = [file_log for file_log in glob.glob('*device1*')]
df = pd.DataFrame()
for file_log in file_log:
    data = pd.read_csv(file_log,sep='analogue values:',names=['time', 
'col'], engine='python')
    df = data.append(data1)

I transform the data and then it looks like this:
analog1 analog2 analog3 analog4 time 2511 2383 2461 2472 00:00:00.283672 2501 2395 2467 2465 00:00:00.546165 2501 2395 2467 2465 00:00:00.807846 2497 2381 2461 2467 00:00:01.070540 2485 2391 2458 2475 00:00:01.332163

but the problem is, I want the time column to be date time, where the date is the date from the filename it came from.

analog1 analog2 analog3 analog4 datetime 2511 2383 2461 2472 2018-08-31 00:00:00.283672 2501 2395 2467 2465 2018-08-31 00:00:00.546165 2501 2395 2467 2465 2018-08-31 00:00:00.807846 2497 2381 2461 2467 2018-08-31 00:00:01.070540 2485 2391 2458 2475 2018-08-31 00:00:01.332163

1 Answers1

2

You can convert first 10 values from filename by file[:10] to datetime and add to column time converted by to_timedelta.

Then append each DataFrame to list and last use concat

dfs = []
for file in glob.glob('*device1*'):
    data = pd.read_csv(file,sep='analogue values:',names=['time','col'], engine='python')
    data['datetime'] = pd.to_datetime(file[:10]) + pd.to_timedelta(data['time'])
    data = data.drop('time', axis=1)
    dfs.append(data)

df = pd.concat(dfs, ignore_index=True)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Yes! thanks alot i tried a similar thing myself with the data['datetime'] = pd.to_datetime(file[:10]) + pd.to_timedelta(data['time']) but it didnt do it the way i wanted it to. – Martijn van Amsterdam Oct 24 '18 at 13:46