简体   繁体   English

使用groupby将功能应用于熊猫数据框(“索引过多”错误)

[英]Applying function to Pandas dataframe with groupby ('Too many indexers' error)

I am trying to compute mean and var along axis=1 of dataframe using only first k columns (compute as .iloc[:,:-5] ),naively, I would run as: 我试图仅使用前k列(计算为.iloc[:,:-5] )沿数据帧的axis=1计算meanvar ,但我会这样运行:

df.groupby('id').agg([lambda x: x.iloc[:,:-5].mean(axis=1), lambda x: x.iloc[:,:-5].var(axis=1)])

but it throws the 'too many indexers' error. 但它会引发“索引器过多”错误。

EDIT 编辑

Some data: 一些数据:

       0    1    2    3    4    5    6    7    8    9             Q1      Q2      Q3     Q4              id
0    3.0  3.0  4.0  4.0  3.0  3.0  3.0  3.0  3.0  3.0           12.0    0.83    80.0  1.000            11.0
1    3.0  3.0  4.0  4.0  4.0  3.0  3.0  3.0  3.0  3.0           14.0    1.60    80.0  1.000            11.0
2    3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  5.0           13.0    1.40    75.0  1.000            11.0
3    3.0  3.0  4.0  4.0  4.0  3.0  3.0  3.0  3.0  3.0           12.0    0.50    80.0  0.848            11.0
4    3.0  4.0  4.0  4.0  7.0  7.0  5.0  4.0  4.0  2.0           13.0    1.74    70.0  0.883            11.0
13   3.0  3.0  2.0  2.0  2.0  2.0  3.0  2.0  3.0  3.0           12.0    3.67    45.0  1.000            14.0
14   2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0           13.0    3.67    48.0  0.848            14.0
15   2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0           12.0    1.67    70.0  0.848            14.0
16   2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  NaN  2.0           12.0    3.33    60.0  0.848            14.0
17   2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0           12.0    3.33    60.0  0.848            14.0
25   4.0  4.0  6.0  5.0  NaN  6.0  4.0  3.0  NaN  4.0           11.0    3.36    85.0  0.796            17.0
26   4.0  5.0  4.0  7.0  6.0  5.0  4.0  6.0  7.0  5.0            8.0    4.76    50.0  0.725            17.0
27   4.0  4.0  3.0  4.0  5.0  4.0  5.0  3.0  3.0  5.0            9.0    3.33    50.0  0.725            17.0
28   3.0  4.0  4.0  3.0  4.0  4.0  NaN  3.0  NaN  3.0           10.0    3.12    75.0  0.725            17.0
29   3.0  3.0  2.0  NaN  2.0  1.0  NaN  NaN  1.0  2.0           15.0    3.05    79.0  0.725            17.0
39   3.0  3.0  5.0  4.0  4.0  4.0  4.0  4.0  NaN  5.0           12.0    1.19    80.0  0.883            18.0
40   5.0  4.0  5.0  5.0  5.0  5.0  4.0  5.0  7.0  4.0            9.0    1.83    75.0  0.883            18.0
41   5.0  6.0  4.0  4.0  4.0  4.0  4.0  4.0  7.0  7.0           12.0    1.71    35.0  1.000            18.0
42   5.0  5.0  5.0  5.0  4.0  NaN  4.0  4.0  3.0  2.0           13.0    0.86    85.0  1.000            18.0
43   3.0  3.0  3.0  3.0  3.0  3.0  3.0  5.0  3.0  3.0           11.0    1.36    75.0  1.000            18.0
48   1

It seems you need first: 看来您首先需要:

df['m'] = df.iloc[:,:-5].mean(axis=1)
df['v'] = df.iloc[:,:-5].var(axis=1)

and then aggregate if necesary. 然后根据需要进行汇总。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM