Search code examples
pythonpandasdataframedummy-variable

Pandas - Map - Dummy Variables - Assign value of 1


I have two dataframes, x.head() looks like this:

top      mid       adc      support jungle
Irelia   Ahri      Jinx     Janna   RekSai
Gnar     Ahri      Caitlyn  Leona   Rengar
Renekton Fizz      Sivir    Annie   Rengar
Irelia   Leblanc   Sivir    Thresh  JarvanIV
Gnar     Lissandra Tristana Janna   JarvanIV

and dataframe fullmatrix.head() that I have created looks like this:

Irelia  Gnar    Renekton    Kassadin    Sion    Jax Lulu    Maokai  Rumble  Lissandra   ... XinZhao Amumu   Udyr    Ivern   Shaco   Skarner FiddleSticks    Aatrox  Volibear    MonkeyKing
0   0   0   0   0   0   0   0   0   0   0   ... 0   0   0   0   0   0   0   0   0   0
1   0   0   0   0   0   0   0   0   0   0   ... 0   0   0   0   0   0   0   0   0   0
2   0   0   0   0   0   0   0   0   0   0   ... 0   0   0   0   0   0   0   0   0   0
3   0   0   0   0   0   0   0   0   0   0   ... 0   0   0   0   0   0   0   0   0   0
4   0   0   0   0   0   0   0   0   0   0   ...

Now what I cannot figure out is how to assign a value of 1 for each name in the x dataframe to the respective column that has the same name in the fullmatrix dataframe row by row (both dataframes have the same number of rows).


Solution

  • I'm sure this can be improved but one advantage is that it only requires the first DataFrame, and it's conceptually nice to chain operations until you get the desired solution.

    fullmatrix = (x.stack()
                   .reset_index(name='names')
                   .pivot(index='level_0', columns='names', values='names')
                   .applymap(lambda x: int(x!=None))
                   .reset_index(drop=True))
    

    note that only the names that appear in your x DataFrame will appear as columns in fullmatrix. if you want the additional columns you can simply perform a join.