我對以下數據(來自 pandas 數據框)遇到一些困難:Text0 Selected moments from Fifa game t...1 What I learned is that I am ...3 Bill Gates kept telling us it was comi...5 scenario created a month before the...... ...1899 Events for May 19 – October 7 - October CTOvision.com1900 Office of Event Services and Campus Center Ope...1901 How the CARES Act May Affect Gift Planning in ...1902 City of Rohnert Park: Home1903 iHeartMedia, Inc.我需要提取每行的唯一單詞數(刪除標點符號后)。因此,例如:Unique0 61 63 85 6... ...1899 81900 81901 9 1902 51903 2我嘗試按如下方式進行:df["Unique"]=df['Text'].str.lower()df["Unique"]==Counter(word_tokenize('\n'.join( file["Unique"])))但我沒有得到任何計數,只有一個單詞列表(沒有它們在該行中的頻率)。你能告訴我出了什么問題嗎?
3 回答

飲歌長嘯
TA貢獻1951條經驗 獲得超3個贊
如果不需要計數,請先刪除所有標點符號。杠桿套。str.split.map(set)
會給你一套。計算后面集合中的元素。集合不采用多個唯一元素。
鏈式
df['Text'].str.replace(r'[^\w\s]+', '').str.split().map(set).str.len()
逐步
df[Text]=df['Text'].str.replace(r'[^\w\s]+', '') df['New Text']=df.Text.str.split().map(set).str.len()

GCT1015
TA貢獻1827條經驗 獲得超4個贊
所以,我只是根據評論更新這一點。該解決方案還考慮了標點符號。
df['Unique'] = df['Text'].apply(lambda x: x.translate(str.maketrans('', '', string.punctuation)).strip()).str.split(' ').apply(len)

有只小跳蛙
TA貢獻1824條經驗 獲得超8個贊
嘗試這個
from collections import Counter
dict = {'A': {0:'John', 1:'Bob'},
'Desc': {0:'Bill ,Gates Started Microsoft at 18 Bill', 1:'Bill Gates, Again .Bill Gates and Larry Ellison'}}
df = pd.DataFrame(dict)
df['Desc']=df['Desc'].str.replace(r'[^\w\s]+', '')
print(df.loc[:,"Desc"])
print(Counter(" ".join(df.loc[0:0,"Desc"]).split(" ")).items())
print(len(Counter(" ".join(df.loc[0:0,"Desc"]).split(" ")).items()))
添加回答
舉報
0/150
提交
取消