我有下面的示例数据框架。让我们假设每个字母实际上是一个词。例如,
a = 'ant'
和
b = 'boy'
.
id words
1 [a, b, c, d, e, f, g]
1 [h, I, o]
1
1 [a, b, c]
2 [e, f, g, m, n, q, r, s]
2 [w, j, f]
3 [l, t, m, n, q, s, a]
3 [c, d, e, f, g]
4
4 [f, g, z]
创建上述示例数据帧的代码:
import pandas as pd
d = {'id': [1, 1, 1, 1, 2, 2, 3, 3, 4, 4], 'words': [['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['h', 'I', 'o'], '', ['a', 'b', 'c'], ['e', 'f', 'g', 'm', 'n', 'q', 'r', 's'], ['w', 'j', 'f'], ['l', 't', 'm', 'n', 'q', 's', 'a'], ['c', 'd', 'e', 'f', 'g'], '', ['f', 'g', 'z']]}
df = pd.DataFrame(data=d)
我在上面运行了以下NLP代码来执行以下操作:给我一个从“words”字段中并置在一起的各种3字组合的计数。
from nltk.collocations import *
from nltk import ngrams
from collections import Counter
trigram_measures = nltk.collocations.BigramAssocMeasures()
finder = BigramCollocationFinder.from_documents(df['words'])
finder.nbest(trigram_measures.pmi, 100)
s = pd.Series(df['words'])
ngram_list = [pair for row in s for pair in ngrams(row, 3)]
counts = Counter(ngram_list).most_common()
df = pd.DataFrame.from_records(counts, columns=['gram', 'count'])
假设输出的样本结果如下(数据值是假的):
gram count
a, b, c 13
c, d, e 9
g, h, i 6
q, r, s 1
问题是,我希望结果输出被“id”字段分割。
我想要的样本输出如下(数据是假的和随机的)
:
id gram count
1 a, b, c 13
1 c, d, e 9
1 g, h, i 6
1 q, r, s 1
2 a, b, c 6
2 w, j, f 3
3 l, t, m 4
3 e, f, g 2
4 f, g, z 1
我如何做到这一点?…按“ID”字段获取结果?