gpt4 book ai didi

python - Pandas 数据框 : count number of IDs based on occurence of words in a text column

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:54 27 4
gpt4 key购买 nike

我有一个像这样的 Pandas 数据框:

id   comment

1 its not proper
2 improvement needed
3 organization is proper
4 registration not done
5 timelines not proper

对于这些词组 ['proper','organization','done'] 我想计算它们出现的 id 的数量。所以输出应该是这样的:

proper         3
organization 1
done 1

我已经尝试过使用 for 循环:

word_list = ['proper','organization','done']
final _list = {'proper':0,'organization':0,'done':0}
for index,row in data.iterrows():
for word in word_list:
if word in row['comment'].split(' '):
final_list[word] += 1

有没有什么方法可以不使用任何 for 循环...

最佳答案

您可以使用 str.contains 并在 words 的列表理解中对 bool 值求和

In [23]: words = ['proper','organization','done']

In [24]: pd.DataFrame([[wrd, df['comment'].str.contains(wrd).sum()] for wrd in words])
Out[24]:
0 1
0 proper 3
1 organization 1
2 done 1

关于python - Pandas 数据框 : count number of IDs based on occurence of words in a text column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33407223/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com