I am trying to do something similar to the example worked here at Geeks for Geeks.
In the example there they work through a dataframe of NBA (basketball) players and find the 5 smallest players (by height or weight) in the league.
All of this makes sense and I am able to apply it easily to the dataset I am working with until I want to do some more precise data slicing. For example, I want to find the 3 smallest players on each NBA team.
I would like the data output to be as follows:
| Team | Player | Weight | Other Data x | Other Data y |
|---|---|---|---|---|
| Team A | Player 1 | smallest | xxxxxx | yyyyyy |
| Team A | Player 2 | 2nd small | xxxxxx | yyyyyy |
| Team A | Player 3 | 3rd small | xxxxxx | yyyyyy |
| -------- | ----------- | ----------- | ------------- | ------------- |
| Team B | Player 1 | smallest | xxxxxx | yyyyyy |
| Team B | Player 2 | 2nd small | xxxxxx | yyyyyy |
| Team B | Player 3 | 3rd small | xxxxxx | yyyyyy |
| -------- | ----------- | ----------- | ------------- | ------------- |
| Team C | Player 1 | smallest | xxxxxx | yyyyyy |
| Team C | Player 2 | 2nd small | xxxxxx | yyyyyy |
| Team C | Player 3 | 3rd small | xxxxxx | yyyyyy |
| -------- | ----------- | ----------- | ------------- | ------------- |
I have tried doing both of the follow and neither of the work the way I would like.
df2 = df1.groupby('Team').nsmallest(3, ['Weight'])df2 = df1.groupby('Team')['Weight'].nsmallest(3)
Any suggestions of how I can do multiple cuts of data? I also want to keep the data in all of the other columns, just not the team / player / weight data.