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

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

使用pandas python按組計算字符串中的單詞數

使用pandas python按組計算字符串中的單詞數

慕的地8271018 2022-07-19 11:00:58
我想通過 standard_supplier_name 獲得前 20 個常用詞以及計數這是我的數據框的樣子: https ://i.stack.imgur.com/kOrjz.png我希望我的輸出看起來像這樣: https ://i.stack.imgur.com/07iaA.png有人可以幫我解決這個問題嗎?
查看完整描述

2 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

讓我們試試這個:


df = pd.DataFrame({'standard_supplier_name':['ibl america', 'b.v. shie van'], 

                  'index':['aa, human, tag, bachulovius,slam, family, member, aa , human,tag',

                         'aanbrengen, looproute, bij']})


df = df.set_index('index')


#input df


df.reset_index()\

  .set_index('standard_supplier_name')['index'].str.split(',')\

  .explode().str.strip().value_counts()

輸出:


human          2

tag            2

aa             2

looproute      1

bij            1

aanbrengen     1

member         1

family         1

bachulovius    1

slam           1

Name: index, dtype: int64


查看完整回答
反對 回復 2022-07-19
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

這是我對您要達到的目標的最佳猜測。


將來,嘗試提供數據集和最小的可重現示例。


desc = ["aa, bc, cd, cd, aa, bb", "xy, jk, yb"]

comp = ["abc", "xyz"]


df = pd.DataFrame({"comp": comp, "desc":desc})

#split words into tokens

df["desc"] = df.desc.str.split(", ")

#stack words per group

stacked = pd.DataFrame(df.desc.tolist(), index=df.comp).stack().reset_index()

stacked.columns = ["comp", "drop", "token"] 


#group by comp and token and count occurances

stacked.groupby(["comp","token"]).size().reset_index()


查看完整回答
反對 回復 2022-07-19
  • 2 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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