0
我希望在多個條件下合併通過sql獲取的數據幀。Python Pandas:在多個條件下合併數據幀
- df1:第一個df包含Customer ID,Cluster ID和Customer Zone ID。
- 第二個df包含投訴ID,註冊號。
的DF1和DF2如下所示:
DF1
Customer ID Cluster ID Customer Zone ID
CUS1001.A CUS1001.X CUS1000
CUS1001.B CUS1001.X CUS1000
CUS1001.C CUS1001.X CUS1000
CUS1001.D CUS1001.X CUS1000
CUS1001.E CUS1001.X CUS1000
CUS2001.A CUS2001.X CUS2000
DF2:
Complain ID RegistrationNumber Status
CUS3501.A 99231 open
CUS1001.B 21340 open
CUS1001.X0 open
我要合併用以下的條件這兩個數據幀:
if(Complain ID == Customer ID):
Merge on Customer ID
Elif(Complain ID == Cluster ID):
Merge on Customer ID
Elif (Complain ID == Customer Zone ID):
Merge on Customer ID
Else:
Merge empty row.
最終結果應該是這樣的:
Customer ID Cluster ID Customer Zone ID Complain ID Regi ID Status
CUS1001.A CUS1001.X CUS1000 CUS1001.X0 open
CUS1001.B CUS1001.X CUS1000 CUS1001.B 21340 open
CUS1001.C CUS1001.X CUS1000 CUS1001.X0 open
. . . . . .
. . . . . .
CUS2001.A CUS2001.X CUS2000 0 0 0
請幫忙!
任何替代方法可以做同樣的事情嗎? –
@ShawnNash你可以'def'你自己的功能這個特殊的'merge'過程 – Wen
@ShawnNash如果你仍然感興趣,你可以檢查我的更新 – Wen