7

I have a pandas dataframe: key val

A    1

A    2

B    1

B    3

C    1

C    4

I want to get do some dummies like this:

A  1100

b  1010

c  1001
Suever
  • 64,497
  • 14
  • 82
  • 101
burness duan
  • 111
  • 1
  • 5
  • 1
    Please define dummies – Suever Apr 16 '16 at 16:04
  • It's rather unclear what you're trying to do. Can you show us any code what you've tried so far? – ventiseis Apr 16 '16 at 19:01
  • I'm sorry, it's my first question on the stack overflow. For example, the val of A is 1,2 so I want to get the 1100(the first and second index to be 1). I know the `get_dummies` In pandas but it can only get the 1000 or 0100. – burness duan Apr 18 '16 at 01:33

2 Answers2

18

Here is something that I used for my case and tried to apply on yours as far as I could understand the problem

df
  key1  key2
0    A     1
1    A     2
2    B     1
3    B     3
4    C     1
5    C     4

pd.get_dummies(df, columns=['key2']).groupby(['key1'], as_index=False).sum()

Output:

  key1  key2_1  key2_2  key2_3  key2_4
0    A     1.0     1.0     0.0     0.0
1    B     1.0     0.0     1.0     0.0
2    C     1.0     0.0     0.0     1.0
ayhan
  • 70,170
  • 20
  • 182
  • 203
Shivam Shakti
  • 196
  • 1
  • 5
-2

If you just want to create dummy data in a pandas DF, you can use the below.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(100, 4), columns=['A', 'B' ,'C' ,'D'])

df


#Result will look like the following
"""
       A         B         C         D
0  0.777877  1.513237  1.521985  2.017665
1 -1.247366  0.874258  0.986717 -1.148804
...........continued for N rows

"""