[英]Count the number of Occurrence of Values based on another column
我有一個關於根據其他列的總和創建pandas數據幀的問題。
例如,我有這個數據幀
Country | Accident
England Car
England Car
England Car
USA Car
USA Bike
USA Plane
Germany Car
Thailand Plane
我想根據國家/地區的所有事故的總和值制作另一個數據框。 我們將忽略事故的類型,同時根據國家總結事故。
我的願望數據框架看起來像這樣
Country | Sum of Accidents
England 3
USA 3
Germany 1
Thailand 1
您可以使用groupby
方法。
示例 -
In [36]: df.groupby(["country"]).count().sort_values(["accident"], ascending=False).rename(columns={"accident" : "Sum of accidents"}).reset_index()
Out[36]:
country Sum of accidents
0 England 3
1 USA 3
2 Germany 1
3 Thailand 1
說明 -
df.groupby(["country"]). # Group by country
count(). # Aggregation function which counts the number of occurences of country
sort_values( # Sorting it
["accident"],
ascending=False).
rename(columns={"accident" : "Sum of accidents"}). # Renaming the columns
reset_index() # Resetting the index, it takes the country as the index if you don't do this.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.