Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 满足groupby条件后如何返回索引?_Python_Pandas - Fatal编程技术网

Python 满足groupby条件后如何返回索引?

Python 满足groupby条件后如何返回索引?,python,pandas,Python,Pandas,我有一个大的df.pandas,我想用一个函数来groupby一列,在另一列中计算uniqe值,然后返回max count值的索引,如下所示: def groupby_fun(): x = df.groupby(by=df['Column1'])['Column2'].nunique() return [x.max()].index[0] groupby_fun() 我得到这个错误: 'builtin_function_or_method' object is not subs

我有一个大的df.pandas,我想用一个函数来groupby一列,在另一列中计算uniqe值,然后返回max count值的索引,如下所示:

def groupby_fun():
    x = df.groupby(by=df['Column1'])['Column2'].nunique()
    return [x.max()].index[0]
groupby_fun()
我得到这个错误:

'builtin_function_or_method' object is not subscriptable
请问我做错了什么?

添加了Nickil Maveli建议的
.idxmax()

df.groupby('Column1')['Column2'].nunique().idxmax()

x.max()周围的方括号是什么?加上它应该是index(),而不是方括号这不是问题所在,尽管我将其更改为返回x.max.index(),同样的错误,但改为:“function”对象没有属性“index”
df.groupby('Column1')['Column2'].nunique().idxmax()
。旁白-
max
返回标量值而不是序列对象,因此无法通过该值访问索引。