I have a dataframe in Python Pandas which I've shifted to obtain a 3 week view of a specific column:
id | state | week_number | year |
---|---|---|---|
A | 1 | 52 | 2022 |
A | 1 | 51 | 2022 |
A | 0 | 50 | 2022 |
df["current_week"] = df.state
df["week_2"] = df.state.shift(-1)
df["week_3"] = df.state.shift(-2)
To end up with:
id | state | week_number | year | current_week | week_2 | week_3 |
---|---|---|---|---|---|---|
A | 1 | 52 | 2022 | 1 | 1 | 0 |
A | 1 | 51 | 2022 | 1 | 0 | 0 |
A | 0 | 50 | 2022 | 0 | 0 | 0 |
A | 0 | 47 | 2022 | 0 | 0 | 0 |
A | 0 | 46 | 2022 | 0 | 0 | 0 |
This achieves my requirement - however, some IDs will not have the full 52 weeks. I'd like to generate new rows for each ID that has missing weeks and impute 0 for state, only adding missing weeks - so leaving the existing as they are. So as an example, let's say ID A has week 52, 51 and 50 but is missing 49 and 48 - I'd like to achieve this:
id | state | week_number | year | current_week | week_2 | week_3 |
---|---|---|---|---|---|---|
A | 1 | 52 | 2022 | 1 | 1 | 0 |
A | 1 | 51 | 2022 | 1 | 0 | 0 |
A | 0 | 50 | 2022 | 0 | 0 | 0 |
A | 0 | 49 | 2022 | 0 | 0 | 0 |
A | 0 | 48 | 2022 | 0 | 0 | 0 |
A | 0 | 47 | 2022 | 0 | 0 | 0 |
A | 0 | 46 | 2022 | 0 | 0 | 0 |
It seems extremely simple, I'm sure it's my lack of ability to concisely explain the problem which has prevented me from finding an answer on Google. Any help is greatly appreciated.