I have a table with keywords associated with articles, looks like this:
article_id keyword
1 A
1 B
1 C
2 A
2 B
2 D
3 E
3 F
3 D
I need to get a sort of a pivot table:
A B C D E F
A - 2 1 1 0 0
B - - 1 1 0 0
C - - - 0 0 0
D - - - - 1 1
E - - - - - 1
F - - - - - -
It means, that the pair (A, B)
occurs in two articles (#1 and #2), the pair (A, C)
occurs in just one article (#1), etc.
What is the most Pythonic way to do that?
I tried Pandas pivot tables, but with no success so far. Just can't get how to connect the keywords and article ids.
This question Create adjacency matrix for two columns in pandas dataframe doesn't solve the problem.