burness duan
burness duan

Reputation: 111

How to group by and dummies in pandas

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

Upvotes: 7

Views: 8795

Answers (2)

Shivam Shakti
Shivam Shakti

Reputation: 196

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

Upvotes: 18

user6194984
user6194984

Reputation:

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

"""

Upvotes: -2

Related Questions