I had a doubt about groupby operations. Lets suppose I have grouped my data based on one column and got 5 groups as output. I know that we can iterate over these groups and apply functions to them as a whole. But can I access the elements of each group (for example I have 5 groups each having 5 rows, can I access those rows one by one). I want to apply a function that compares two rows of a single group based on one or more parameters of the rows belonging to that group.

Year   Rank    Final_Score    Team
2000    1           50          A
2000    2           35          B       
2000    3           65          C
2003    3           20          B
2003    1           53          C
2003    2           44          A

If I have grouped by years such that the two groups would be group for year 2000 and 2003 respectively. I want to compare row1 and row2 of group 2000 by comparing their attribute Final Score and for this I need to access each row in the group. Can I do that by grouby()?

  • $\begingroup$ Something with df.groupby().shift()? $\endgroup$ – Quang Hoang Feb 16 at 14:27
  • $\begingroup$ Just looked up what shift() is but it won't solve my problem because I want to access the rows in a group formed by groupby $\endgroup$ – Pari Ganjoo Feb 16 at 14:34
  • 2
    $\begingroup$ Yes, that's exactly what groupby().shift() does? Are you sure you weren't looking at df.shift()? $\endgroup$ – Quang Hoang Feb 16 at 14:39
  • $\begingroup$ Okay I may have mistaken between the two of them then. Thanks for the help! $\endgroup$ – Pari Ganjoo Feb 16 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.