Home > Enterprise >  Adding rows and incrementing datetime values to pandas data frame based on column value?
Adding rows and incrementing datetime values to pandas data frame based on column value?

Time:04-22

I have a pandas data frame with three columns. Column A is of datetime type, Column B type is integer, Column C type is float but not important for this question. My goal is to add rows to the data frame determined by each value in Column B, while incrementing the datetime in A by one hour each.

For example, given this data frame:

A                   B   C
4/18/2021 1:00:00   3   1
4/20/2021 5:00:00   2   0

produces this output:

A                   B   C
4/18/2021 1:00:00   3   1
4/18/2021 2:00:00   3   1
4/18/2021 3:00:00   3   1
4/20/2021 5:00:00   2   0
4/20/2021 6:00:00   2   0

A naive approach would be to loop through each row of the data frame adding new rows iteratively, but I prefer to use a more efficient solution to manipulate the data.

CodePudding user response:

One option is a list comprehension, followed by explode:

(df
.assign(
    A = [pd.date_range(start = a, periods = b, freq='1H') 
         for a, b in zip(df.A, df.B)])
.explode('A')
)
                    A  B  C
0 2021-04-18 01:00:00  3  1
0 2021-04-18 02:00:00  3  1
0 2021-04-18 03:00:00  3  1
1 2021-04-20 05:00:00  2  0
1 2021-04-20 06:00:00  2  0
  • Related