Skip to content Skip to sidebar Skip to footer

Grouped Feature Matrix In Python #2- Follow Up

It's not too different from before. We can start with the sample data: DataFrame1: Name No. Comment Bob 2123320 Doesn't Matter Joe 2832883

Solution 1:

Try:

df_out = df_out[df_out.groupby(['Name'])['No.'].transform(lambda x: x.nunique() > 1)]\
   .set_index(['Name','No.'])['Comment'].str.get_dummies()\
   .reindex(df_out.Comment, fill_value=0, axis=1)\
   .sum(level=[0,1])\
   .unstack()\
   .swaplevel(0,1,axis=1)\
   .sort_index(1)

print(df_out)

Output:

No.     2139300                                                                \
Comment Awesome Doesn't Matter Good Great Irrelevant Perfect Regardless Solid   
Name                                                                            
John          10001000   

No.                        2234903                                       \
Comment Something Whatever Awesome Doesn't Matter Good Great Irrelevant   
Name                                                                      
John            0000000   

No.                                                  
Comment Perfect Regardless Solid Something Whatever  
Name                                                 
John          11000

Post a Comment for "Grouped Feature Matrix In Python #2- Follow Up"