دنبال کننده ها

۱۳۹۶ آذر ۹, پنجشنبه

python - convert dataframe to the heatmap matrix?

[ad_1]



I have a panda data frame with the structure like this:



df = pd.DataFrame('entry': [['A','B','C'],['A','B','E','D'],['C'],['D','A', 'B'],['D','C','E'],['E','A']])


which gives:



 entry
0 [A, B, C]
1 [A, B, E, D]
2 [C]
3 [D, A, B]
4 [D, C, E]
5 [E, A]


I want to convert it to the square matrix, size of unique symbols seen in the data frame (in this case it is 5: 'A','B','C','D','E') where every intersection is the frequency of this pair been seen together (like here the pair [A,B] is seen together 3 times, pair [D,A] - only once. If there are 3 or more symbols together, I want all the combinations to be considered.) So the output is something like this:



 A B C D E
A 3 1 1 1
B 1 1
C 1 1
D 2
E


I am quite a beginner in the field, tried to write a loop that goes through all the combinations. Have a problem with the fact that there may be any amount of items inside the entry.



Thank you for the help!




[ad_2]

لینک منبع