我有一个名为df
的Pandas 数据帧,看起来像这样
name test_type test_number correct
joe 0 1 1
joe 0 2 0
joe 1 1 0
joe 1 2 1
joe 0 1 1
joe 0 2 1
jim 1 1 0
jim 1 2 1
jim 0 1 0
jim 0 2 1
jim 1 1 0
jim 1 2 0
我想要一个按name
分组的数据集,并提取correct
×test_type
的平均值(作为单个值)以及correct by test_type
和test_number
的平均值(作为numpy
数组).
以下是我需要的:
name correct_0 correct_1 correct_0_by_tn correct_val_1_by_tn
joe 0.75 0.5 [1, 0.5] [0, 1]
jim 0.5 0.25 [0, 1] [0, 0.5]
我一直在使用df.groupby(["name", "test_type"]).correct.mean().reset_index()
和df.groupby(["name", "test_type", "test_number"]).correct.mean().reset_index()
,但我无法1)像我想要的那样提取test_number
的平均值作为一个数组,2)在一个连贯的数据帧中组织输出.
提前谢谢.