I have a Columns line
Score
Country Batsmen Runs India Sachin 15000
India Virat 12000
Australia Smith 10000 Australia Warner 8000 Australia Ricky 11000 NewZealad Williamson 8000 England Butler 5000 England Stokes 9000 ....
....
I want to find the top3 countries with overall Runs. My Output should be
Australia 29000 India 27000 England 14000 NewZealand 8000
I am not able to find an exact expression that does it.
I tried this Score.groupby('Country')['Runs'].sum()
This give me the whole output in a Series but not in ascending order as it puts the Country and Runs together as one Column.
What is the best way to Sort the Country on the Basis of overall runs scored by all batsmen. I see methods of Grouping one column and Sorting the other, but here I want it to be sorted on some operations on the Runs Columns (sum here)
Test data:
df = pd.DataFrame(
{
"Country": [
"India",
"India",
"Australia",
"Australia",
"Australia",
"New Zealand",
],
"Batsman": ["Sachin", "Virat", "Smith", "warner", "Ricky", "Wiliamson"],
"Run": [15000, 12000, 10000, 8000, 1100, 8000],
}
)
Pandas groupby with sort:
df.groupby("Country").sum().sort_values("Run", ascending=False)
Output:
Run
Country
India 27000
Australia 19100
New Zealand 8000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.