Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 标记句子和计算数据帧中的数字_Python_Pandas - Fatal编程技术网

Python 标记句子和计算数据帧中的数字

Python 标记句子和计算数据帧中的数字,python,pandas,Python,Pandas,我有这个数据框 index sentences category 1 the side effects are terrible ! SSRI 2 They are killing me,,, I want to stop SNRI 3 I need to cont

我有这个数据框

index      sentences                                            category
1          the side effects are terrible !                         SSRI
2          They are killing me,,, I want to stop                   SNRI
3          I need to contact my physicians ?                        SSRI
4          How to stop it.. I am surprised because of its effect.   SSRI
我需要标记这个句子,然后计算每个类别的标记数。我知道我可以用下面的代码来做。但我不知道如何计算代币的数量

df['tokenized_sents'] = df.apply(lambda row: nltk.word_tokenize(row['sentences']), axis=1)

任何建议

都不如使用相同的应用方法来计数那么简单

df['len_tokens'] = df['tokenized_sents'].apply(lambda x: len(x))