I have a dataframe as follows
id1, id2, thumb_up
1, 1, 1
1, 2, -1
1, 3, 1
2, 1, 1
2, 3, -1
and so on
I want to create the following dataframe
id1, thumb_up_prob
1, 0.66 ( 2 positive feedback and 1 negative = 2/3)
2, 0.5 (1 positive and 1 negative feedback = 1/2)
and so on.. so basically i want to groupby id1 and compute the probability. How do i do this in pandas.
df.thumb_up
is equal to 1 groupby
df.id1
bool
is subset of int
we cal call mean
on it df.thumb_up.eq(1).groupby(df.id1).mean()
With apply
function and lambda
# df
df = pd.DataFrame({'id1':[1,1,1,2,2], 'id2':[1,1,3,1,3], 'thumb_up':
[1,-1,1,1,-1]})
# probability
df.groupby('id1').apply(lambda x: x[x>0].count()/len(x))
output:
id1 id2 thumb_up
id1
1 1.0 1.0 0.666667
2 1.0 1.0 0.500000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.