我有一个数据框df
为:
Acct_Id Acct_Nm Srvc_Id Phone_Nm Phone_plan_value Srvc_Num
51 Roger 789 Pixel 30 1
51 Roger 800 iPhone 25 2
51 Roger 945 Galaxy 40 3
78 Anjay 100 Nokia 50 1
78 Anjay 120 Oppo 30 2
32 Rafa 456 HTC 35 1
我想转换数据帧,以便每个Acct_Id
和Acct_Nm
可以有1行,如下所示:
Acct_Id Acct_Nm Srvc_Num_1 Srvc_Num_2 Srvc_Num_3
Srvc_Id Phone_Nm Phone_plan_value Srvc_Id Phone_Nm Phone_plan_value Srvc_Id Phone_Nm Phone_plan_value
51 Roger 789 Pixel 30 800 iPhone 25 945 Galaxy 40
78 Anjay 100 Nokia 50 120 Oppo 30
32 Rafa 456 HTC 35
我不确定如何在熊猫中实现同样的目标。
答案 0 :(得分:2)
更像一个pivot
问题,但需要swaplevel
和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.0 456.0
51 Roger Pixel 30.0 789.0
78 Anjay Nokia 50.0 100.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.0 800.0
78 Anjay Oppo 30.0 120.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.0 945.0
78 Anjay None NaN NaN
这是pivot_table
pd.pivot_table(df,index=['Acct_Id','Acct_Nm'],columns=['Srvc_Num'],values=['Phone_Nm','Phone_plan_value','Srvc_Id'],aggfunc='first')
答案 1 :(得分:1)
怎么样:
df.set_index(['Acct_Id', 'Acct_Nm', 'Srvc_Num']).unstack().swaplevel(0, 1, axis = 1).sort_index(axis = 1)