Find all matching groups in a list of lists with pandas

Viewed 202

I would like to find all cases for all ids in a Pandas DataFrame. What would be an efficient solution? I have around 10k of records and it is processed server-side. Would it be a good idea to create a new DataFrame, or is there a more efficient data structure I can use? A case is satisfied when an id contains all names in a case.

Input (Pandas DataFrame)

id | name |
-----------
1  | bla1 |
2  | bla2 |
2  | bla3 |
2  | bla4 |
3  | bla5 |
4  | bla9 |
5  | bla6 |
5  | bla7 |
6  | bla8 |

Cases

names [
  [bla2, bla3, bla4], #case 1
  [bla1, bla3, bla7], #case 2
  [bla3, bla1, bla6], #case 3
  [bla6, bla7] #case 4
]

Needed output (unless there is a more efficient way)

id | case1 | case2 | case3 | case4 |
------------------------------------
1  | 0     | 0     | 0     | 0     |
2  | 1     | 0     | 0     | 0     |
3  | 0     | 0     | 0     | 0     |
4  | 0     | 0     | 0     | 0     |
5  | 0     | 0     | 0     | 1     |
6  | 0     | 0     | 0     | 0     |
1 Answers
Related