I have a data frame as below:
user | profit
-------------
Anna | 1.0
Bell | 2.0
Anna | 2.0
Chad | 5.0
Bell | 4.0
Anna | 3.0
that I need to compute each row's mean value on the users' level, that is, each time I see the same user I compute his/her profit mean thus far.
For instance, Anna's first profit mean is 1.0 and her second profit mean becomes 1.5, and so on.
The desired result looks like:
user | profit | mean
--------------------
Anna | 1.0 | 1.0
Bell | 2.0 | 2.0
Anna | 2.0 | 1.5
Chad | 5.0 | 5.0
Bell | 4.0 | 3.0
Anna | 3.0 | 2.0
Any suggestions to do so in Python/Pandas?
import pandas as pd
record = pd.DataFrame({
"user": ("Anna", "Bell", "Anna", "Chad", "Bell", "Anna"),
"profit": (1.0, 2.0, 2.0, 5.0, 4.0, 3.0)
})
Thanks!