Vb407
Vb407

Reputation: 1607

Create dynamic columns in dataframe using pandas

How to create dynamic columns from this pandas dataframe.

 Name, Sex
    a, M
    b, F
    c, M
    d, F

Expected dataframe:

Name, M, F
a, 1, 0
b, 0, 1
c, 1, 0
d, 0, 1

I have tried pandas.pivot() but of no use, could you guys suggest something.

Upvotes: 1

Views: 1389

Answers (2)

akuiper
akuiper

Reputation: 214927

You can create a count variable based on the two columns and then do the pivoting, something like:

import pandas as pd
df.groupby(["Name", "Sex"]).size().unstack(level = 1, fill_value = 0)

# Sex   F   M
#Name       
#   a   0   1
#   b   1   0
#   c   0   1
#   d   1   0

Another option is to use crosstab from pandas:

import pandas as pd
pd.crosstab(df['Name'], df['Sex'])

# Sex   F   M
#Name       
#   a   0   1
#   b   1   0
#   c   0   1
#  d    1   0

Upvotes: 1

user2285236
user2285236

Reputation:

Use get dummies:

pd.concat([df['Name'], df['Sex'].str.get_dummies()], axis=1)
Out: 
    Name   F   M
0      a   0   1
1      b   1   0
2      c   0   1
3      d   1   0

df['Sex'].str.get_dummies() generates the dummies:

df['Sex'].str.get_dummies()
Out: 
    F   M
0   0   1
1   1   0
2   0   1
3   1   0

and then you can use pd.concat to combine the result with the name column.

Upvotes: 5

Related Questions