Issue
I have a set of data about football:
I sorted it by how many matches were played at each ground:
fb2 = fb.groupby(by=['Ground'])['Ground'].count()
print(fb2)
Output:
Ground
Aberdeen 11
Abu Dhabi 37
Adelaide 82
Ahmedabad 24
Albion 5
..
Vijayawada 1
Visakhapatnam 11
Wellington 56
Whangarei 1
Worcester 3
Name: Ground, Length: 173, dtype: int64
Now I want to sort the results, in descending order of how many matches were played at each ground.
I am very new to python...any help would be much appreciated!
Solution
If you use named aggregation, you can specify a column name to sort on:
fb2 = (
fb
.groupby('Ground', as_index=False)
.agg(n_matches=('Ground', 'count'))
.sort_values('n_matches', ascending=False)
)
Answered By - Eric Truett
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.