[英]Sum groupby column values returned by value_counts()
我正在按人口統計和評級條件返回企業功能的評級。
我如何將所有變量的“count”列的 int64 值求和,同時在輸出中包含變量名稱,例如Design 8
, Food 1
?
這個問題提到了轉換到索引然后按索引選擇。
這個問題看起來像 SQL 的類似問題。
目前,我可以通過將 melt 代碼塊分配給變量prod
進行查詢,然后編寫類似這樣的內容。 prods[prods.rating == 2]
。
示例數據:
Customer Type Age Satisfaction Design Food Wi-Fi Service Distance
Disloyal 28 Not Satisfied 0 1 2 2 13.5
Loyal 30 Satisfied 5 3 5 4 34.2
Disloyal 36 Not Satisfied 2 0 2 4 55.8
# Cols I want to see the ratings for
ranked_cols = [
"Design",
"Food",
"Wi-Fi",
"Service",
]
# Select the relevant customers
sub = df[
(df["Customer Type"] == "Disloyal")
& (df["Satisfaction"] == "Not Satisfied")
& df["Age"].between(30, 40)
]
(
sub.melt(value_vars=ranked_cols)
.groupby("variable")
.value_counts()
.to_frame()
.reset_index()
.rename(columns={"value": "rating", 0: "count"})
)
[Out]
variable rating count
0 Design 2 5
1 Food 0 1
2 Service 4 1
3 Wi-Fi 2 3
4 Design 1 3
df.groupby("variable").sum()["count"]
輸出:
variable
Design 8
Food 1
Service 1
Wi-Fi 3
Name: count, dtype: int64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.