我在 pandas 中有以下数据框,其中employee每一行都有一个唯一索引 (),还有一个组标签type:
df = pandas.DataFrame({"employee": ["a", "b", "c", "d"], "type": ["X", "Y", "Y", "Y"], "value": [10,20,30,40]})
df = df.set_index("employee")
我想对员工进行分组type,然后计算每种类型的统计数据。我怎样才能做到这一点并获得最终的数据框type x statistic,例如type x (mean of types)?我尝试使用groupby:
g = df.groupby(lambda x: df.ix[x]["type"])
result = g.mean()
ix这是低效的,因为它引用了df每一行的索引- 有没有更好的方法?