1

I have a pandas data frame like this:

Original data set

and I need to convert it into the below:

Output data set

So, the unique values in column 'camps' part of the original data set, gets turned into columns with values 0 and 1 for each id. How can I achieve this using Pandas or anything else in Python? Any help is greatly appreciated. Thanks

Below is the code to create the original data set:

import pandas as pd
cust = {'id': [212175, 286170, 361739, 297438, 415712, 415777, 261700, 314624, 170365, 333254],
        'camps': [':_Camp1_CC SC_Statemt_CCSTMT', ':_Camp1_CC SC_Statemt_CCSTMT', ':_Camp1_Free_DS0742203H_BD03', 
                       ':_Camp1_Over_EO3982112A_BD07', ':_Camp1_Over_EO4022202A_BD16', ':_Camp1_Over_EO4022202A_BD16', 
                      ':_Camp1_AS07_DS0722204H_DD02', ':_Camp1_AS07_DS0722204H_DD02', ':_Camp1_AS07_DS0722204H_DD02', 
                      ':_Camp1_AS07_DS0722204H_DD02']}

cust_df = pd.DataFrame(cust)
SAJ
  • 368
  • 1
  • 2
  • 14

2 Answers2

1

try

pd.get_dummies()

for your reference: https://www.geeksforgeeks.org/how-to-create-dummy-variables-in-python-with-pandas/

lunaayase
  • 45
  • 8
1

A possible solution, based on pd.crosstab:

pd.crosstab(cust_df.id, cust_df.camps)

Output:

camps   :_Camp1_AS07_DS0722204H_DD02  ...  :_Camp1_Over_EO4022202A_BD16
id                                    ...                              
170365                             1  ...                             0
212175                             0  ...                             0
261700                             1  ...                             0
286170                             0  ...                             0
297438                             0  ...                             0
314624                             1  ...                             0
333254                             1  ...                             0
361739                             0  ...                             0
415712                             0  ...                             1
415777                             0  ...                             1
PaulS
  • 21,159
  • 2
  • 9
  • 26