BMichell
BMichell

Reputation: 3771

Writing function to apply to Pandas GroupBy

Following from this post: Returning subset of each group from a pandas groupby object I'm trying to write a function to apply to a groupby object in pandas

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)

I'm getting a bit lost with how index works in this case. In the example I have the ID returned is 1189 - but the line were I do df.iloc[1189] returns an error that positional indicator is out of bounds.

My understanding was that the index should be preserved during the groupby which is what my ID= line is telling me. But then it's not clear to me why the iloc call is then throwing an error.

My groupby is by two columns - not sure if that's a factor.

Ben

Upvotes: 1

Views: 70

Answers (1)

jezrael
jezrael

Reputation: 863801

You can use loc for select by labels, because idxmin return indices, not positions:

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

Upvotes: 1

Related Questions