0

I had 1 dataset where all the services were appended into a list, so I converted them to rows as I am not aware of how to make them as column and use boolean as value which has been described below.

I tried other threads answer but could not reach to the final solution as I want my value to be boolean and I tried giving it True/False in a list but as we know the in order to evaluate True or False there should so logic, and I have no clue how to do that.

pivoted = df.pivot(index='Name', columns='Services', values=[True, False])\
            .reset_index()
pivoted.columns.name=None

I have a dataset as below:

===========================================
Address          Name     Phone      Service
abc, hyd India   abc      8923       PPF    
abc, hyd India   abc      8923       EPF
abc, hyd India   abc      8923       DAP
xyz, hyd india   xyz      7576       EPF
===============================================

I want the result to be as:

==================================================
Address          Name     Phone    PPF     EPF   DAP
abc, hyd India   abc      8923     True    True  True
xyz, hyd india   xyz      7576     False   True  False
=================================================================
Rahul Agarwal
  • 3,743
  • 6
  • 24
  • 40

1 Answers1

2

Look like you want one-hot encoding, try pd.get_dummies() ...

Service_one_hot = pd.get_dummies(df.Service, dtype=bool)
df = df.drop(columns=['Service'])
df = df.join(Service_one_hot)
df.groupby(['Address', 'Name', 'Phone'], as_index=False).sum()

result:

          Address Name  Phone    DAP   EPF    PPF
0  abc, hyd India  abc   8923   True  True   True
1  xyz, hyd india  xyz   7576  False  True  False

You may check out this as well: How can I one hot encode in Python?

Fortissimo
  • 36
  • 4