Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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 编写要应用于Pandas GroupBy的函数_Python_Pandas - Fatal编程技术网

Python 编写要应用于Pandas GroupBy的函数

Python 编写要应用于Pandas GroupBy的函数,python,pandas,Python,Pandas,下面是本文的内容:我正在尝试编写一个函数,以应用于pandas中的groupby对象 def group_by_function(df): ID = df.loc[df.Parameter_1==0].Parameter_2.idxmin() df_2 = df.iloc[ID].Parameter print(ID) return df_2 df.groupby(by=['Column1', 'Column2']).apply(group_b

下面是本文的内容:我正在尝试编写一个函数,以应用于pandas中的groupby对象

def group_by_function(df):
    ID = df.loc[df.Parameter_1==0].Parameter_2.idxmin()        
    df_2 = df.iloc[ID].Parameter

    print(ID)
    return df_2

df.groupby(by=['Column1', 'Column2']).apply(group_by_function)
在这种情况下,我对索引的工作方式有点迷茫。在这个示例中,我返回的ID是1189,但行WARE I do df.iloc[1189]返回一个错误,即位置指示器超出范围

我的理解是,索引应该在groupby期间保留,这是我的ID=line告诉我的。但是我不清楚为什么iloc调用会抛出一个错误

我的groupby由两列组成-不确定这是否是一个因素


Ben

您可以使用
loc
按标签选择,因为
idxmin
返回索引,而不是位置:

df_2 = df.loc[ID, 'Parameter']

你能提供一些数据以便我们能更详细地阐述。。。?