第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

在 Dataframe 中查找句子中的多個單詞并轉(zhuǎn)換為分數(shù)總和

在 Dataframe 中查找句子中的多個單詞并轉(zhuǎn)換為分數(shù)總和

UYOU 2021-06-12 14:09:25
我有以下數(shù)據(jù)框:    Sentence0   Cat is a big lion1   Dogs are descendants of wolf2   Elephants are pachyderm3   Pachyderm animals include rhino, Elephants and hippopotamus我需要創(chuàng)建一個 python 代碼,它查看上面句子中的單詞,并根據(jù)以下不同的數(shù)據(jù)框計算每個單詞的總和。Name          Scorecat             1dog             2wolf            2lion            3elephants       5rhino           4hippopotamus    5例如,對于第 0 行,分數(shù)將為 1(貓)+ 3(獅子)= 4我希望創(chuàng)建一個如下所示的輸出。    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條經(jīng)驗 獲得超1個贊

首先,您可以嘗試一種基于splitandmap的方法,然后使用 計算分數(shù)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 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號