[英]How to get frequency count for a column value, sorted by aa categorical value in another column
I have a pandas dataframe that includes two columns, vessel name and delay indicator.我有一个 pandas dataframe 包括两列,船只名称和延迟指示器。 Vessel name is a string name of a vessel, and delay indicator is either a 0 or 1 (boolean).
船只名称是船只的字符串名称,延迟指示符是 0 或 1(布尔值)。
My DataFrame:我的 DataFrame:
df = pd.DataFrame({
"Vessel.Name": ["Spirit of British Columbia", "Queen of New Westminster", "Spirit of Vancouver Island", "Coastal Celebration", "Spirit of British Columbia"],
"Delay.Indicator":[0, 0, 0, 1, 0]
})
How it looks:它的外观:
Vessel.Name Delay.Indicator
Spirit of British Columbia 0
Queen of New Westminster 0
Spirit of Vancouver Island 0
Coastal Celebration 1
Spirit of British Columbia 0
My goal is to get a DataFrame that includes each different ship name, and two new columns indicating its count, and its total number of "1" in delay indicator, for each different ship name.我的目标是获得一个 DataFrame,其中包括每个不同的船名,以及两个新列,指示其计数,以及延迟指示器中的“1”总数,用于每个不同的船名。 Not sure if there are pandas methods for this or if I should iterate through python lists?
不确定是否有 pandas 方法或者我是否应该遍历 python 列表?
A simple groupby with aggregate functions applied should do the trick:应用聚合函数的简单 groupby 应该可以解决问题:
df.groupby("Vessel.Name")["Delay.Indicator"].agg(['count', sum])
Output: Output:
count sum
Vessel.Name
Coastal Celebration 1 1
Queen of New Westminster 1 0
Spirit of British Columbia 2 0
Spirit of Vancouver Island 1 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.