Mann-Whitney U大Pandas数据帧测试

2024-09-28 01:33:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个与此类似的大数据帧:

In [1]: grades
Out[1]: 
                          course1  course2
school  class  student                    
school1 class1 student1         2        2
               student2         3        2
               student3         1        3
               student4         3        1
               student5         3        1
...                           ...      ...
        class3 student86        3        1
               student87        2        2
               student88        1        1
               student89        3        3
               student90        0        1

[90 rows x 2 columns]

我想计算样本学校和每个子样本班级的成绩的曼惠特尼排名测试。如何使用pandas和scipy.stats.mannwhitneyu在不遍历数据帧的情况下实现这一点?在


Tags: 数据inoutstudentclassgradesschoolclass1
1条回答
网友
1楼 · 发布于 2024-09-28 01:33:16

两个调用的函数是

index = pandas.MultiIndex.from_product([
    ['school{0}'.format(n) for n in xrange(3)],
    ['class{0}'.format(n) for n in xrange(3)],
    ['student{0}'.format(n) for n in xrange(10)]
])
d = pandas.DataFrame({'course1': np.random.randint(0, 10, 90), 'course2': np.random.randint(0, 10, 90)},
                     index=index)

然后按学校计算曼惠特尼U:

^{pr2}$

按班级来做:

>>> d.groupby(level=[0, 1]).apply(lambda t: stats.mannwhitneyu(t.course1, t.course2))
school0  class0     (38.5, 0.200247279189)
         class1     (37.0, 0.169040187814)
         class2     (46.5, 0.409559639829)
school1  class0     (33.5, 0.110329749527)
         class1     (47.5, 0.439276896563)
         class2    (30.0, 0.0684355963119)
school2  class0     (47.5, 0.439438219083)
         class1     (43.0, 0.308851989782)
         class2     (34.0, 0.118791221444)
dtype: object

levels参数中的数字表示多重索引的级别。因此,按学校按0级分组,按学校/班级组合按0级和1级分组。在

相关问题 更多 >

    热门问题