0

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?

komodovaran_
  • 1,940
  • 16
  • 44
  • And also https://stackoverflow.com/questions/12200693/python-pandas-how-to-assign-groupby-operation-results-back-to-columns-in-parent, now that I check. But I feel my post describes the problem more concisely. – komodovaran_ Nov 17 '17 at 15:48
  • I disagree. It is a duplicate - please mark it as so if you agree... if you feel like the original post with all of the answers can be made more concise, feel free to suggest an edit to the question. – miradulo Nov 17 '17 at 15:48

0 Answers0