1

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

1
  • Can you provide some data so we can elaborate furter...? Commented May 22, 2019 at 8:36

1 Answer 1

1

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

df_2 = df.loc[ID, 'Parameter']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.