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

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

在 Dataframe 中查找句子中的多個單詞并轉換為分數總和

在 Dataframe 中查找句子中的多個單詞并轉換為分數總和

UYOU 2021-06-12 14:09:25
我有以下數據框:    Sentence0   Cat is a big lion1   Dogs are descendants of wolf2   Elephants are pachyderm3   Pachyderm animals include rhino, Elephants and hippopotamus我需要創建一個 python 代碼,它查看上面句子中的單詞,并根據以下不同的數據框計算每個單詞的總和。Name          Scorecat             1dog             2wolf            2lion            3elephants       5rhino           4hippopotamus    5例如,對于第 0 行,分數將為 1(貓)+ 3(獅子)= 4我希望創建一個如下所示的輸出。    Sentence                                                      Value0   Cat is a big lion                                                41   Dogs are descendants of wolf                                     42   Elephants are pachyderm                                          53   Pachyderm animals include rhino, Elephants and hippopotamus      14
查看完整描述

3 回答

?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

首先,您可以嘗試一種基于splitandmap的方法,然后使用 計算分數groupby。


v = df1['Sentence'].str.split(r'[\s.!?,]+', expand=True).stack().str.lower()

df1['Value'] = (

    v.map(df2.set_index('Name')['Score'])

     .sum(level=0)

     .fillna(0, downcast='infer'))

df1

                                            Sentence  Value

0                                  Cat is a big lion      4

1                       Dogs are descendants of wolf      4  # s/dog/dogs in df2  

2                            Elephants are pachyderm      5

3  Pachyderm animals include rhino, Elephants and...     14


查看完整回答
反對 回復 2021-06-15
  • 3 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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