亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

NLP 生成按列中的值分組的并置三元組數據框

NLP 生成按列中的值分組的并置三元組數據框

HUWWW 2021-11-02 09:52:11
我有以下示例數據框。讓我們假設每個字母實際上都是一個單詞。例如,a = 'ant'和b = 'boy'。id  words1   [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 代碼以執行以下操作: 給我一個從“單詞”字段并置在一起的各種 3 詞組合的計數。from nltk.collocations import *from nltk import ngramsfrom collections import Countertrigram_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                       13c, d, e                       9g, h, i                       6q, r, s                       1問題是我希望將結果輸出按“id”字段拆分。 我想要的樣本輸出如下(數據是假的和隨機的):id   gram                          count 1    a, b, c                       131    c, d, e                       91    g, h, i                       61    q, r, s                       12    a, b, c                       62    w, j, f                       33    l, t, m                       43    e, f, g                       24    f, g, z                       1我如何實現這一目標?...通過“id”字段獲取結果?
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號