'Repeat row based on quarter except with change of one column
I would like to repeat the same rows based on the QTR info, but only change the QTR column on each successive row only as long as the QTR is not the current quarter. For example I have a dataset,
A = np.random.randint(10, size=(4,5))
df = pd.DataFrame(A)
df['QTR'] = ['2021Q4','2021Q3', '2022Q1','2022Q2']
df
I would like to get three row with the column QTR: '2021Q4', '2022Q1', '2022Q2' for the first row where other column values remains same, and so on for the other rows. I am a newbie in python, not sure how to proceed for it? The expected output would be like
A1 = np.array([[8,1,6],[8,1,6], [8,1,6], [4,5,2], [4,5,2], [4,5,2], [4,5,2], [8,4,0],[8,4,0], [3,4,7]])
A1
df1 = pd.DataFrame(A1)
df1['QTR'] = ['2021Q4', '2022Q1', '2022Q2', '2021Q3', '2021Q4', '2022Q1', '2022Q2', '2022Q1','2022Q2', '2022Q2']
df1
Solution 1:[1]
IIUC, you can use:
(df
.assign(QTR=df['QTR'].apply(lambda x: pd.date_range(start=x, freq='Q', periods=3).to_period('Q')))
.explode('QTR')
)
output:
0 1 2 3 4 QTR
0 7 7 4 0 8 2021Q4
0 7 7 4 0 8 2022Q1
0 7 7 4 0 8 2022Q2
1 2 5 7 8 9 2021Q3
1 2 5 7 8 9 2021Q4
1 2 5 7 8 9 2022Q1
2 9 9 6 9 3 2022Q1
2 9 9 6 9 3 2022Q2
2 9 9 6 9 3 2022Q3
3 1 8 9 3 4 2022Q2
3 1 8 9 3 4 2022Q3
3 1 8 9 3 4 2022Q4
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
