+ 2

Pandas. After separating rows into groups, how to return the result of only one specific group?

The code is: nba[["Team", "Salary"]].groupby("Team").max() What if I need the max() of one specific team from nba.csv? How to indicate it? I've tried to use: nba.get_group("TeamName") but it doesn't work after max().

1st Feb 2020, 7:30 PM
LeiaR
2 Réponses
+ 3
I have done some research about this. groupby() actually returns a GroupBy object, but the aggregation functions (like sum()) return a DataFrame that has labelled indices by default. So you have two choices: groups = nba[["Team", "Salary"]].groupby("Team") print(groups.get_group("TeamName").max()) # or print(groups.max().loc["TeamName"]) https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html
2nd Feb 2020, 9:23 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thank you so much, that worked perfectly!
2nd Feb 2020, 9:37 AM
LeiaR