Pandas Pivot Table Without Aggregating
I have a dataframe df as: Acct_Id Acct_Nm Srvc_Id Phone_Nm Phone_plan_value Srvc_Num 51 Roger 789 Pixel 30 1 51 Roger 800
Solution 1:
More like a pivot
problem , but need swaplevel
and sort_index
df.set_index(['Acct_Id','Acct_Nm','Srvc_Num']).\
unstack().\
swaplevel(1,0,axis=1).\
sort_index(level=0,axis=1).add_prefix('Srvc_Num_')
Out[289]:
Srvc_Num Srvc_Num_1 \
Srvc_Num_Phone_Nm Srvc_Num_Phone_plan_value Srvc_Num_Srvc_Id
Acct_Id Acct_Nm
32 Rafa HTC 35.0456.051 Roger Pixel 30.0789.078 Anjay Nokia 50.0100.0
Srvc_Num Srvc_Num_2 \
Srvc_Num_Phone_Nm Srvc_Num_Phone_plan_value Srvc_Num_Srvc_Id
Acct_Id Acct_Nm
32 Rafa None NaN NaN
51 Roger iPhone 25.0800.078 Anjay Oppo 30.0120.0
Srvc_Num Srvc_Num_3
Srvc_Num_Phone_Nm Srvc_Num_Phone_plan_value Srvc_Num_Srvc_Id
Acct_Id Acct_Nm
32 Rafa None NaN NaN
51 Roger Galaxy 40.0945.078 Anjay None NaN NaN
And here is pivot_table
pd.pivot_table(df,index=['Acct_Id','Acct_Nm'],columns=['Srvc_Num'],values=['Phone_Nm','Phone_plan_value','Srvc_Id'],aggfunc='first')
Post a Comment for "Pandas Pivot Table Without Aggregating"