Search code examples
pythonpandasdata-manipulationexpanddays

Expanding a data set from months to days in Python for non-DateTime type data


How do I expand this monthly table(Table A) into a daily table(Table B) that spreads revenue across the 30 day period?

Table A

index   Month   Revenue ($)
0          1    300
1          2    330
2          3    390

(Assuming each month has 30 days)

Table B

index   Month   Day Revenue ($)
0         1      1   10
1         1      2   10
2         1      3   10
...      ...    ... ...
30        2      1   11
31        2      2   11
...      ...    ... ...
60        3      1   13
...      ...    ... ...
89        3     30   13

Solution

  • Try:

    df = pd.concat([df]*30).assign(Revenue=lambda x: x['Revenue'] / 30).sort_values('Month')

    Create the days column

    df['day'] = [i for i in range(1, 31)] * number_of_months

    print(df)

        Month  Revenue  day
    0       1     10.0    1
    1       1     10.0    2
    2       1     10.0    3
    3       1     10.0    4
    4       1     10.0    5
    ..    ...      ...  ...
    85      3     13.0   26
    86      3     13.0   27
    87      3     13.0   28
    88      3     13.0   29
    89      3     13.0   30