python - 计算python数据框中短语的出现次数
问题描述
我有一个这样的df:
names = ["Internal medicine, Gastroenterology", "Internal medicine, Family and general medicine, Endocrinology", "Pediatrics, Medical genetics, Laboratory medicine", "Internal medicine"]
df = pd.DataFrame(names, columns=['names'])
我想知道每个医学术语出现的频率。例如这里
- 内科:3
- 胃肠病学:1
- ETC
它适用Counter
于单词,但我如何让它适用于诸如“内科”之类的短语?“,”分隔短语。
解决方案
如果你想使用collections.Counter
,你可以这样做:
In [1945]: from collections import Counter
In [1946]: d = Counter(df['names'].str.split(", ").explode().tolist())
In [1947]: d
Out[1947]:
Counter({'Internal medicine': 3,
'Gastroenterology': 1,
'Family and general medicine': 1,
'Endocrinology': 1,
'Pediatrics': 1,
'Medical genetics': 1,
'Laboratory medicine': 1})
推荐阅读
- php - 函数($value)是什么以及在哪里使用:SomeClass
- google-chrome-headless - 如何在无头 chrome 实例上设置缩放级别?
- java - BpmnError 在活动中不起作用
- microcontroller - 控制 4 位 7 段 LED 接口 MCU 8051
- multithreading - OpenGL PBO 映射缓冲区:多线程解包慢,memcpy 快
- php - PHP - 命令行中的服务器名称
- php - Laravel $刀片之谜中的错误
- javascript - 如果在表单验证中的 if 条件之后,我怎样才能使它变得更好
- r - 在闪亮的不工作中部署 R 程序
- php - 碳逐月获得数字回报三月 2