+ 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().
2 Answers
+ 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
+ 1
Thank you so much, that worked perfectly!