0

I am trying to build a binary matrix, where the rows are groups and columns are items that could be in the groups. There is a 1 in the column if the item is present and a 0 if not. Is there a way to do this efficiently?

For example, if I have a dictionary like this:

{device1 : (news1, news2, ...), device2 : (news 2, news 4, ...)...}

Is there a quick way to turn it into a matrix where device1, device2, etc are rows and news1, news2, etc. are columns with 1s or 0s?

I have tried iterating over columns, but its inefficient and I think there is an easier way to do it!

tawab_shakeel
  • 3,701
  • 10
  • 26

1 Answers1

0

I believe this is a duplicate:

converting dictionary to binary in python

test_dict = {"device1" : ("news1", "news2"), 
             "device2" : ("news2", "news3"), 
             "device3" : ("news2", "news4")}

df = pd.concat([pd.Series(v, name=k).astype(str) for k, v in test_dict.items()], 
               axis=1)
df = pd.get_dummies(df.stack()).sum(level=1).clip_upper(1)

Output of df:

         news1  news2  news3  news4
device1      1      1      0      0
device2      0      1      1      0
device3      0      1      0      1
kasool
  • 53
  • 1
  • 1
  • 6