I have fitted a Hidden Markov Model to some of my data, which looks like this
> print(hmm_df)
signal time hmm_state
0 8.248691 0 1
1 3.776487 1 1
2 3.943656 2 1
3 2.854063 3 1
...
50 15.600341 50 4
51 14.295500 51 4
52 12.714964 52 4
53 14.301315 53 4
And now I want the mean of each state, which is easily done with
groupby_result = hmm_df.groupby(["hmm_state"], as_index=False)["signal"].mean()
And now I get some means for every state, which is something like this.
hmm_state signal
1 1 4.948970
...
4 4 15.293361
But if I want to plot this, it would be nice to have the mean value for every state, for every datapoint, like so:
signal time hmm_state mean_signal
0 8.248691 0 1 4.948970
1 3.776487 1 1 4.948970
2 3.943656 2 1 4.948970
3 2.854063 3 1 4.948970
...
50 15.600341 50 4 15.293361
51 14.295500 51 4 15.293361
52 12.714964 52 4 15.293361
53 14.301315 53 4 15.293361
How can I "uncollapse" the grouped result back into the original dataframe?