I have a data set as I've shown below:
It shows which book is sold by which shop.
import pandas as pd
books = {'shop': ["A", "B", "C", "D", "E", "A", "B", "C", "D",],
'book_id': [1, 1, 2, 3, 3, 3, 4, 5, 1,]
}
df = pd.DataFrame(books, columns = ['shop', 'book_id'])
Here is the print:
shop book_id
0 A 1
1 B 1
2 C 2
3 D 3
4 E 3
5 A 3
6 B 4
7 C 5
8 D 1
In the data set,
- shop A sells 1, 3
- shop B sells 1, 4
- shop C sells 2, 5
- shop D sells 3, 1
- shop E sells only 3
So now, I want to calculate the jaccard index here. For instance, let's take shop A and shop B. There are three different books that are sold by A and B (book 1, book 3, book 4). However, only one product is sold by both shops (this is product 1). So, the Jaccard index here should be 33.3% (1/3).
Here is the sample of the desired data:
result = {'shop_1': ["A", "B", "A", "C", "A", "D", "A", "E",],
'shop_2': ["B", "A", "C", "A", "D", "A", "E", "A",],
'jaccard': [33.3, 33.33, 0, 0, 100, 100, 50, 50,]
}
desired_df = pd.DataFrame(result, columns = ['shop_1', 'shop_2', 'jaccard'])
Print
shop_1 shop_2 jaccard
0 A B 33.30
1 B A 33.33
2 A C 0.00
3 C A 0.00
4 A D 100.00
5 D A 100.00
6 A E 50.00
7 E A 50.00
. . . .
. . . .
. . . .
Can someone help me to do this? Is there any library to implement Jaccard Index?