给出以下数据:
Class Name
====== =============
Math John Smith
-------------------------
Math Jenny Simmons
-------------------------
English Sarah Blume
-------------------------
English John Smith
-------------------------
Chemistry Roger Tisch
-------------------------
Chemistry Jenny Simmons
-------------------------
Physics Sarah Blume
-------------------------
Physics Jenny Simmons
我有一个类和名字的列表,就像这样:
[
{class: 'Math', student: 'John Smith'},
{class: 'Math', student: 'Jenny Simmons'},
{class: 'English', student: 'Sarah Blume'},
{class: 'English', student: 'John Smith'},
{class: 'Chemistry', student: 'John Smith'},
{class: 'Chemistry', student: 'Jenny Simmons'},
{class: 'Physics', student: 'Sarah Blume'},
{class: 'Physics', student: 'Jenny Simmons'},
]
我想创建一个邻接矩阵,作为输入,它将具有以下结构,显示每对班级之间的共同学生人数:
我怎样才能在python/pandas中以最高效的方式做到这一点?我的名单上有大约1900万对这样的班级/学生(约240MB)。你知道吗
您可以像这样准备邻接矩阵的数据:
完整输出如下所示:
然后可以使用@piRSquared的方法来旋转它,或者像这样做:
或者,如果您还需要名称:
相关问题 更多 >
编程相关推荐