Is there a method for creating dataframe of dummy variables from a dictionary of lists?

Viewed 1177

In pandas I have a dictionary that looks like the one below:

{'Anemones & allies': ['Carnivore'],
'Ants, bees & wasps': ['Omnivore',  'Herbivore',  'Nectar',  'Insects', 'Parasite'],
'Beetles & bugs': ['Herbivore', 'Carnivore', 'Nectar', 'Insects'],
'Birds': ['Carnivore'],
'Fishes': ['Carnivore', 'Plankton or Particles']}

I want to convert it into a DataFrame in which you can see, what the animaltype could possibly eat. So it would look similar to the image below:

What the DataFrame should look like.

When trying to generate such a table I got the feeling that I was doing it in an incorrect way because I needed quite some lines of code. So my question is, is there a nice function to map this dictionary to a DataFrame so it looks something like the above table?

2 Answers

Simplest Way
Using pd.str.get_dummies

dct = {
    'Anemones & allies': ['Carnivore'],
    'Ants, bees & wasps': ['Omnivore',  'Herbivore',  'Nectar',  'Insects', 'Parasite'],
    'Beetles & bugs': ['Herbivore', 'Carnivore', 'Nectar', 'Insects'],
    'Birds': ['Carnivore'],
    'Fishes': ['Carnivore', 'Plankton or Particles']
}

pd.Series(dct).str.join('|').str.get_dummies()

                    Carnivore  Herbivore  Insects  Nectar  Omnivore  Parasite  Plankton or Particles
Anemones & allies           1          0        0       0         0         0                      0
Ants, bees & wasps          0          1        1       1         1         1                      0
Beetles & bugs              1          1        1       1         0         0                      0
Birds                       1          0        0       0         0         0                      0
Fishes                      1          0        0       0         0         0                      1

More Complicated
But probably recommended

from sklearn.preprocessing import MultiLabelBinarizer

dct = {
    'Anemones & allies': ['Carnivore'],
    'Ants, bees & wasps': ['Omnivore',  'Herbivore',  'Nectar',  'Insects', 'Parasite'],
    'Beetles & bugs': ['Herbivore', 'Carnivore', 'Nectar', 'Insects'],
    'Birds': ['Carnivore'],
    'Fishes': ['Carnivore', 'Plankton or Particles']
}

s = pd.Series(dct)

mlb = MultiLabelBinarizer()

d = mlb.fit_transform(s)
c = mlb.classes_
pd.DataFrame(d, s.index, c)

                    Carnivore  Herbivore  Insects  Nectar  Omnivore  Parasite  Plankton or Particles
Anemones & allies           1          0        0       0         0         0                      0
Ants, bees & wasps          0          1        1       1         1         1                      0
Beetles & bugs              1          1        1       1         0         0                      0
Birds                       1          0        0       0         0         0                      0
Fishes                      1          0        0       0         0         0                      1

Create the DataFrame from dict(d) then using get_dummies

pd.get_dummies(pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in d.items() ])).stack()).sum(level=1)
Out[130]: 
                    Carnivore  Herbivore  Insects  Nectar  Omnivore  Parasite  \
Anemones & allies           1          0        0       0         0         0   
Ants, bees & wasps          0          1        1       1         1         1   
Beetles & bugs              1          1        1       1         0         0   
Birds                       1          0        0       0         0         0   
Fishes                      1          0        0       0         0         0   
                    Plankton or Particles  
Anemones & allies                       0  
Ants, bees & wasps                      0  
Beetles & bugs                          0  
Birds                                   0  
Fishes                                  1  
Related