I have data in the following format.
date group ret
1986-01-31 1 1.3
1986-01-31 1 0.9
1986-01-31 2 1.4
1986-01-31 2 1.6
1986-01-31 2 1.5
1986-01-31 3 1.1
1986-02-28 2 1.3
1986-02-28 2 1.1
I want to get the average return per date and group, which I get by doing:
output = df.groupby(['date', 'group'])['ret'].mean() + 1
output = output.reset_index()
Which gives the following output:
date group ret
1986-01-31 1 1.1
1986-01-31 2 1.5
1986-01-31 3 1.1
1986-02-28 2 1.2
However, since no "ret" was given at date 1986-02-28 for class 1 and 3, there is no row in the output for class 1 and 3 on this date. What I would like is that for any combination of dates and classes where no return is given in the original dataframe, this combination gets and output of "1" in the output. So, the required output is:
date group ret
1986-01-31 1 1.1
1986-01-31 2 1.5
1986-01-31 3 1.1
1986-02-28 1 1
1986-02-28 2 1.2
1986-02-28 3 1
What would be a good solution for this problem? Thanks in advance!