Search code examples
pythonpandasdataframedatetimestrptime

Stacking multiple dataframes together for different timestamp format into one timestamp


I have multiple data frames each having data varying from 1 to 1440 minute (one day).Each dataframes are alike and same columns and same length. The time column values are in hhmm format. Lets say df_A has the data of 1st day, that is 2021-05-06 It looks like this.

>df_A
timestamp    col1   col2.....  col80
0
1
2
.
.
.
2359

And the next day's data is in df_B which is also the same. The date is 2021-05-07

>df_B
timestamp    col1   col2.....  col80
0
1
2
.
.
.
2359

How could I stack these together one under another and create one dataframe while identifying each rows with a column having values in format like YYYYMMDD HH:mm. Which somewhat will look like this:

>df
timestamp      col1    col2.....  col80
20210506 0000
20210506 0001
.
.
20210506 2359
20210507 0000
.
.
20210507 2359

How could I achieve this while dealing with multiple data frames at ones?


Solution

  • df_A = pd.DataFrame(range(0, 10), columns=['timestamp'])
    df_B = pd.DataFrame(range(0, 10), columns=['timestamp'])
    
    
    df_A['date'] = pd.to_datetime('2021-05-06 ' + 
                                  df_A['timestamp'].astype(str).str.zfill(4), format='%Y-%m-%d %H%M')
    
    df_B['date'] = pd.to_datetime('2021-05-07 ' + 
                                  df_A['timestamp'].astype(str).str.zfill(4), format='%Y-%m-%d %H%M')
    
    df_final = pd.concat([df_A, df_B])
    
    df_final
    
        timestamp   date
    0   0   2021-05-06 00:00:00
    1   1   2021-05-06 00:01:00
    2   2   2021-05-06 00:02:00
    3   3   2021-05-06 00:03:00
    4   4   2021-05-06 00:04:00
    5   5   2021-05-06 00:05:00
    6   6   2021-05-06 00:06:00
    7   7   2021-05-06 00:07:00
    8   8   2021-05-06 00:08:00
    9   9   2021-05-06 00:09:00
    0   0   2021-05-07 00:00:00
    1   1   2021-05-07 00:01:00
    2   2   2021-05-07 00:02:00
    3   3   2021-05-07 00:03:00
    4   4   2021-05-07 00:04:00
    5   5   2021-05-07 00:05:00
    6   6   2021-05-07 00:06:00
    7   7   2021-05-07 00:07:00
    8   8   2021-05-07 00:08:00
    9   9   2021-05-07 00:09:00