我有兩列——一列是句子,另一列是單個單詞。
| 句子 | 單詞 |
|---|---|
| “這樣的一天!這是美好的一天” | “美麗的” |
| “這樣的一天!這是美好的一天” | “天” |
| “我為難過的天氣感到難過” | “天氣” |
| “我為難過的天氣感到難過” | “傷心” |
我想計算“句子”列中“單詞”列的頻率并實作此輸出:
| 句子 | 單詞 | n |
|---|---|---|
| “這樣的一天!這是美好的一天” | “美麗的” | 1 |
| “這樣的一天!這是美好的一天” | “天” | 2 |
| “我為難過的天氣感到難過” | “天氣” | 1 |
| “我為難過的天氣感到難過” | “傷心” | 2 |
我試過:
ok = []
for l in [x.split() for x in df['Sentence']]:
for y in df['word']:
ok.append(l.count(y))
但是它不會停止運行并且需要很長時間,因此對于我的實際資料集是不可行的,因為它有 50k 行。
任何人都可以幫助實作這一目標?
uj5u.com熱心網友回復:
你可以這樣做zip
df['new'] = [x.count(y) for x, y in zip(df.Sentence,df.word)]
df
Out[419]:
Sentence word new
0 Such a day! It's a beautiful day out there beautiful 1
1 Such a day! It's a beautiful day out there day 2
2 I am sad by the sad weather weather 1
3 I am sad by the sad weather sad 2
uj5u.com熱心網友回復:
嘗試使用pandas.apply:
df['n'] = df.apply(lambda r: r['Sentence'].count(r['word']), axis=1)
結果:
Sentence word n
0 Such a day! It's a beautiful day out there beautiful 1
1 Such a day! It's a beautiful day out there day 2
2 I am sad by the sad weather weather 1
3 I am sad by the sad weather sad 2
uj5u.com熱心網友回復:
您可以使用以下代碼計算字串中的字串
# define string
string = "This is how you count same word of your defined string to another string using python"
substring = "string"
count = string.count(substring)
# print count
print(f"The count of the word {substring} is:", count)
輸出:字串的計數為:2
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/467619.html
上一篇:如何測驗僅設定屬性的駱駝路線
