Dilip Kumar
Dilip Kumar

Reputation: 39

How to merge multiple dummy variables columns which were created from a single categorical variable into single column in python?

I am working on IPL dataset which has many categorical variables one such variable is toss_winner. I have created dummy variable for this and now I have 15 columns with binary values. I want to merge all these column into single column with numbers 0-14 each number representing IPL team.

Screenshot of the dummy variables

Upvotes: 2

Views: 3948

Answers (1)

Shubham Sharma
Shubham Sharma

Reputation: 71689

IIUC, Use:

df['Team No.'] = dummies.cumsum(axis=1).ne(1).sum(axis=1)

Example,

df = pd.DataFrame({'Toss winner': ['Chennai', 'Mumbai', 'Rajasthan', 'Banglore', 'Hyderabad']})
dummies = pd.get_dummies(df['Toss winner'])
df['Team No.'] = dummies.cumsum(axis=1).ne(1).sum(axis=1)

Result:

# print(dummies)
   Banglore  Chennai  Hyderabad  Mumbai  Rajasthan
0         0        1          0       0          0
1         0        0          0       1          0
2         0        0          0       0          1
3         1        0          0       0          0
4         0        0          1       0          0

# print (df)
  Toss winner  Team No.
0     Chennai         1
1      Mumbai         3
2   Rajasthan         4
3    Banglore         0
4   Hyderabad         2

Upvotes: 1

Related Questions