DAY 13
0
AI & Data

## Counter-快速統計元素個數的神器

Counter是在colletions裡面的一個函數，可以用來計算任何可迭代容器(list, string)內的元素個數

### 統計list內元素次數

``````from collections import Counter
A = ["Alice", "Bob", "Bob", "Bob", "Carol"]
Counter(A)
``````

``````Counter({'Alice': 1, 'Bob': 3, 'Carol': 1})
``````

#### Counter類型可以當作dict來呼叫

``````from collections import Counter
A = ["Alice", "Bob", "Bob", "Bob", "Carol"]
count_A = Counter(A)
print("Bob:", count_A["Bob"])
print("Amy:", count_A["Amy"]) # 不存在的key會回傳0而非跳error
``````

``````Bob: 3
Amy: 0
``````

### 統計str內各字元次數

``````B = "Aaaaaa~"
Counter(B)
``````

``````Counter({'A': 1, 'a': 5, '~': 1})
``````

### 統計DataFrame內元素個數

``````import pandas as pd
``````

``````count_class = Counter(df["class"])
count_class
``````

``````Counter({'unacc': 1210, 'acc': 384, 'vgood': 65, 'good': 69})
``````

### 如何用出現次數再產生一個DataFrame表格

#### 可以使用`from_dict()`的方法產生

``````df2 = pd.DataFrame.from_dict(count_class,orient='index',columns=["Count"])
df2
``````

#### 因為Counter可以當作dict處理，直接分別取出keys和values

``````df2 = pd.DataFrame({"class":count_class.keys(),
"count": count_class.values()})
df2
``````

## 統計多欄組合出現頻率——使用groupby和size

``````df.groupby(["class", "safety"]).size().reset_index(name="次數")
``````