如何通过从数据框列中分离男性/女性来找到95%的置信区间?

时间:2017-11-05 19:37:43

标签: python dataframe confidence-interval

Give a 95% confidence interval for the average rating for male reviewers, and do the same for female
##reviewers.


group2 = bigdataframe[['rating']].groupby(bigdataframe['gender'])
group2.count()
FN  =   25740
MN  =   74260

group2.mean()
F =     3.531507
M =     3.529289

group2.std()
FS   =  1.170951
MS   =  1.109556

F - 1.96(FS/(np.sqrt(NF)))
F + 1.96(FS/(np.sqrt(NF)))

M - 1.96(MS/(np.sqrt(NM)))
M + 1.96(MS/(np.sqrt(NM)))


My error: 'float' object is not callable

首先,我使用groupby根据每个性别统计评论。然后我能够使用mean / std函数来获得公式所需的数字。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

使用agg功能的这种功能可能更适合您的应用。您甚至可以编写要聚合的自定义函数。但在这里我创建了上置信区间值。在agrgegate结果中引用列时,您需要使用元组。抱歉我的样本量很小,违反了正常的假设)!

import numpy as np
import pandas as pd
sx = np.array(['M','M','M','F','F','F'])
val = np.random.normal(0,1,6)
df = pd.DataFrame({'sex':sx, 'value':val})
gp = df.groupby('sex')
result = gp.agg(['mean','std','count'])
result[('value','upper_ci')] = result[('value', 'mean')] + 1.96*np.divide(result[('value','std')], np.sqrt(result[('value','count')]))