I have a dataset with dates and data points for that specific date (d1, d2, d3, etc.) for every stock for each country. Some datapoints are missing for some stocks within each country and I want to replace them with average for those stocks in other countries
date stock d1 d2 d3 country
12.94 xyz corp 12 3 4 US
12.95 xyz corp 13 NaN 1 US
12.95 123 corp 3 4 12 US
12.94 abc corp 1 3 5 CA
12.94 abc corp NaN 3 4 CA
So, in above data point d2 for xyz on 12.95 needs to be replaced by average of d2 within US for 12.95
I would appreciate any insight on how to do that. I created an index of unique dates and planned on using pivot table where values iterate through various data points such as d1, d2, etc
cnt_avgs=rawdt.pivot_table(values=["d1",index=["country","],aggfunc=np.mean)