I have a dataframe, df, where I would like to fill in missing values in specific columns based on quarters.
Data
type date stat test
aa Q1 2022 20 1
aa Q2 2022 10 2
aa Q3 2022 30 1
bb Q1 2022 30 1
bb Q2 2022 10 1
Desired
type date stat test
aa Q1 2022 20 1
aa Q2 2022 10 2
aa Q3 2022 30 1
aa Q4 2022 0
bb Q1 2022 30 1
bb Q2 2022 10 1
bb Q3 2022 0
bb Q4 2022 0
Doing
Logic:
The pattern is Q1 2022, Q2 2022, Q3 2022 and Q4 2022.
If there is a 'break' in this pattern, the missing data should fill in accordingly with a stat
value of 0.
I believe I can create a dictionary and then combine the impute function
data = { "Q1 2022":0 ,
"Q2 2022":0 ,
"Q3 2022":0 ,
"Q4 2022":0 ,
}
df["type"].fillna("", inplace = True)
df["date"].fillna("", inplace = True) #input dictionary mapping
df["stat"].fillna("0", inplace = True)
Any suggestion is appreciated.