I'm trying to decompose cohort data into time series for further analysis. I'm imagining the algorithm pretty well, but my code doesn't work at all.
The input data in df is like:
| Cohort Day | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 2020-12-27 | 5.87 | 4.9 | 2.89 | 1.47 | 1.38 | 0.95 |
| 2020-12-28 | 13.2 | 3.1 | 0.79 | 1.47 | 1.38 | 0.95 |
I'm trying to decompose it in this format:
| day | sum |
|---|---|
| 2020-12-27 | 5.87 |
| 2020-12-28 | 4.9 |
| 2020-12-29 | 2.89 |
| 2020-12-30 | 1.47 |
| 2020-12-31 | 1.38 |
| 2020-01-01 | 0.95 |
| 2020-12-28 | 13.2 |
| 2020-12-29 | 3.1 |
| 2020-12-30 | 0.79 |
| 2020-12-31 | 1.47 |
| 2020-01-01 | 1.38 |
| 2020-01-02 | 0.95 |
To achieve that I created an empty dataframe test and then I'm using for loop to create a column with dates at first:
for row in test.itertuples():
test[0:5, 0] = df['Cohort Day'] + df.apply(lambda x: int(str(df.iloc[0, 4:].columns)) for x in df.iteritems())
test[0:5, 1] = df[0, 1:].transpose()
But all I receive is an empty test dataframe.
Any suggestions will be appreciated!