python数据帧分组方式

python数据帧分组方式,python,pandas,group-by,dataframe,Python,Pandas,Group By,Dataframe,我有一个数据框,它被一些轴分组,看起来像: subject trialcode latency 14233664 neighbour 1367.319149 nonneighbour 1191.277778 nonsn 1717.916667 nonwords 1447.948718 52602716 neighbour 1297.745098

我有一个数据框,它被一些轴分组,看起来像:

subject     trialcode     latency

14233664    neighbour     1367.319149
            nonneighbour  1191.277778
            nonsn         1717.916667
            nonwords      1447.948718
 52602716   neighbour     1297.745098
            nonneighbour  1262.578947
            nonsn         1358.280000
            nonwords      1898.157895
500051240   neighbour     1249.102041
            nonneighbour  1287.052632
            nonsn         1444.346154
            nonwords      1960.162162
现在我需要按“trialcode”重新分组,并删除“subject”列。我试着用它来做这件事

data_group_mean.drop('subject',1)
但我犯了一个错误

ValueError: labels ['subject'] not contained in axis.
有没有办法按照我刚才提到的方式对这个数据框架进行重新分组?
谢谢

出现错误的原因是因为“subject”现在是您的索引,因为您在其中分组,您可以调用reset\u index来恢复它,然后可以调用:

data_group_mean.groupby('trialcode').mean()

或者在对groupby的初始调用中,将param作为_index=False传递

是否要重新分组?只需调用data\u group\u mean.reset\u index,然后调用data\u group\u mean.groupby'trialcode'。mean@EdChum这是正确的答案。你为什么不回答问题而留下评论?因为在我发布答案之前,你需要先确认一下