我有一个包含12列(药物类别)的数据框架,其中相同的值(药物类别名称)可能出现在不同的列中
DRG01 DRG02 ... DRG11 DRG12
0 AMOXYCILLIN ORAL SOLIDS AMOEBICIDES ORAL SOLIDS ... NaN NaN
1 VITAMIN DROPS NaN ... NaN NaN
2 AMOXYCILLIN ORAL SOLIDS ANTIHISTAMINES ORAL LIQ ... NaN NaN
3 AMOEBICIDES ORAL LIQUID NaN ... NaN NaN
... ... ... ... ... ...
81531 NaN NaN ... NaN NaN
[81532 rows x 12 columns]
我的目标是创建一个矩阵(2D数组)——行和列由唯一的药物类别名称组成(忽略/删除NaN值)。单元格的值是这些药物类别名称在一行中同时出现的次数。基本上,我正在努力实现以下目标:
AMOXYCILLIN ORAL SOLIDS AMOEBICIDES ORAL SOLIDS ANTIHISTAMINES ORALLIQ VITAM..
AMOXYCILLIN ORAL SOLIDS 0 1 1 0
AMOEBICIDES ORAL SOLIDS 1 1 0 0
ANTIHISTAMINES ORAL LIQ 1 0 0 0
VITAMIN DROPS 0 0 0 1
.....
.....
使用itertools.combinations和一些panda函数,您可以很好地完成这项工作:
像这样
相关问题 更多 >
编程相关推荐