[英]How to count the number of occurrences on comma delimited column in Python Pandas
如何計算整個列列表中逗號分隔值的出現次數
數據框是這樣的:
id column
1
2 1
3 1
4 1,2
5 1,2
6 1,2,4
7 1,2,4
8 1,2,4,6
9 1,2,4,6
10 1,2,4,6,8
11 1,2,4,6,8
所需的 output 是:
id column count
1. 10
2 1. 7
3 1. 0
4 1,2. 6
5 1,2. 0
6 1,2,4. 4
7 1,2,4. 0
8 1,2,4,6. 2
9 1,2,4,6. 0
10 1,2,4,6,8 0
11 1,2,4,6,8 0
試過這個:
df = pd.read_csv('parentsplit/parentlist.csv')
df['count'] = df['parent_list'].str.split(',', expand=True).stack().value_counts()
它不工作。
您可以執行以下操作:
df['count'] = df['id'].apply(lambda x: df['column'].fillna('X').str.contains(str(x)).sum())
這基本上是在計算列中每個id
的出現次數。
Output:
id column count
0 1 None 10
1 2 1 8
2 3 1 0
3 4 1,2 6
4 5 1,2 0
5 6 1,2,4 4
6 7 1,2,4 0
7 8 1,2,4,6 2
8 9 1,2,4,6 0
9 10 1,2,4,6,8 0
10 11 1,2,4,6,8 0
拆分並分解列,然后使用value_counts
count
出現次數,然后 map 將計數添加到id
列
s = df['column'].str.split(',').explode().value_counts()
df['count'] = df['id'].astype(str).map(s).fillna(0)
id column count
0 1 None 10.0
1 2 1 8.0
2 3 1 0.0
3 4 1,2 6.0
4 5 1,2 0.0
5 6 1,2,4 4.0
6 7 1,2,4 0.0
7 8 1,2,4,6 2.0
8 9 1,2,4,6 0.0
9 10 1,2,4,6,8 0.0
10 11 1,2,4,6,8 0.0
一種快速的方法是不使用 pandas 方法,而是使用純 python: itertools.chain
和collections.Counter
:
from itertools import chain
from collections import Counter
c = Counter(chain(*df['column'].str.split(',').values))
df['count'] = df['id'].astype(str).map(c)
output:
id column count
0 1 10
1 2 1 8
2 3 1 0
3 4 1,2 6
4 5 1,2 0
5 6 1,2,4 4
6 7 1,2,4 0
7 8 1,2,4,6 2
8 9 1,2,4,6 0
9 10 1,2,4,6,8 0
10 11 1,2,4,6,8 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.