I have two dataframes (df1, df2), df1 containing the student name, topic preference of each student, and df_topics containing topics.
Here is a sample input dataframe:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'Name':['student 1', 'student 2', 'student 3', 'student 4'],
'topics':['algebra; atom; geometry; evolution; food safety',
'chemical reaction; linear algebra; Probability; quantum',
'botany; electricity; mechanics',
'Statistics; botany; number theory; atom; evolution; Probability']})
df2 = pd.DataFrame({'topics':['algebra', 'Probability', 'geometry', 'atom', 'chemical reaction',
'evolution', 'botany', 'quantum'],
'cluster':[0, 0, 0, 1, 1, 2, 2, 3]
})
I want to represent each student by k (here k=4) dimensional binary vector, i.e. if the student has topics in df2, count topics are in each cluster and divide by a total number of topics for this student. for example, student 1 has two topics in cluster 0, algebra and geometry, which divide by 5 (total number of topics for student 1), we get 0.4 and etc.
the results should look like this :

