pandas Python获取若干列表中元素的num个出现次数

inn6fuwd  于 2023-01-04  发布在  Python
关注(0)|答案(3)|浏览(107)

我有4个身体:

C1 = ['hello','good','good','desk']
C2 = ['nice','good','desk','paper']
C3 = ['red','blue','green']
C4 = ['good']

我想定义一个单词列表,对于每个单词-得到每个语料库的出现次数。
l=['好','蓝']
我会得到

res_df =  word. C1. C2. C3. C4
          good.  2. 1.  0.   1
          blue.  0. 0.  1.   0

我的语料库非常大,所以我在寻找有效的方法。什么是最好的方法来做到这一点?
谢啦,谢啦

7kqas0il

7kqas0il1#

您可以使用python库计数器

counts = [[Counter(C)[word] for C in (C1, C2, C3, C4)] for word in l]
res_df = pd.DataFrame(counts, columns=['C1', 'C2', 'C3', 'C4'], index=l)

输出

C1  C2  C3  C4
good   2   1   0   1
blue   0   0   1   0
rsl1atfo

rsl1atfo2#

使用.loc的另一种替代方法:

df = pd.DataFrame({'C1': Counter(C1), 'C2': Counter(C2), 'C3': Counter(C3), 'C4': Counter(C4)}).loc[l].fillna(0).astype('int')

示例如下:

from collections import Counter
import pandas as pd

C1 = ['hello','good','good','desk']
C2 = ['nice','good','desk','paper']
C3 = ['red','blue','green']
C4 = ['good']

l= ['good','blue']

df = pd.DataFrame({'C1': Counter(C1), 'C2': Counter(C2), 'C3': Counter(C3), 'C4': Counter(C4)}).loc[l].fillna(0).astype('int')

print(df)

输出:

C1  C2  C3  C4
good   2   1   0   1
blue   0   0   1   0
unguejic

unguejic3#

一个想法是通过列表过滤值,列表转换为集合,然后按Counter计数,最后传递给DataFrame,并添加0和整数:

from collections import Counter

d = {'C1':C1, 'C2':C2, 'C3':C3, 'C4':C4}

s = set(l)     

df = (pd.DataFrame({k:Counter([y for y in v if y in s]) for k, v in d.items()})
        .fillna(0).astype(int))
print (df)
      C1  C2  C3  C4
good   2   1   0   1
blue   0   0   1   0

如果可能,列表中不存在值:

from collections import Counter

l= ['good','blue','non']

d = {'C1':C1, 'C2':C2, 'C3':C3, 'C4':C4}

s = set(l)     

df = (pd.DataFrame({k:Counter([y for y in v if y in s]) for k, v in d.items()})
        .fillna(0)
        .astype(int)
        .reindex(l, fill_value=0))
print (df)
    
      C1  C2  C3  C4
good   2   1   0   1
blue   0   0   1   0
non    0   0   0   0

相关问题