I am trying to add missing month for each unique id in pandas by filling other columns with previous row values. I have seen this one Pandas: Add data for missing months, but it's for only one id(it doesn't work for multiple id), how can we do the same thing if we have multiple id? For instance I have a data like this
import numpy as np
import pandas as pd
index = [0,1,2,3,4]
id = pd.Series([1,1,1,2,2],index= index)
price = pd.Series([20,41,61,68,10],index= index)
date_month = pd.Series(['2021-06','2021-11','2022-02','2021-04','2021-07'],index= index)
df = pd.DataFrame(id,columns = ["id"])
df["price"] =price
df['date_month'] = date_month
df
But I want the output be like
index = [0,1,2, 3,4,5, 6, 7,8,9,10,11,12]
id = pd.Series([1,1,1,1, 1,1,1,1,1, 2,2,2,2],index= index)
price = pd.Series([20,20,20,20,20, 41,41,41, 61,68,68,68,10],index= index)
date_month = pd.Series(['2021-06', '2021-07','2021-08', '2021-09', '2021-10','2021-11','2021-12', '2022-01', '2022-02','2021-04', '2021-05',
'2021-06','2021-07'],index= index)
df = pd.DataFrame(id,columns = ["id"])
df["price"] =price
df['date_month'] = date_month
df
You can try