Guest User

Untitled

a guest
Dec 14th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. parentData = [(1,’A’,100), (2,’B’,200)]
  2. parentCols = [‘PID’, ‘PATTR1’, ‘PATTR1’]
  3. parentDf = pd.DataFrame.from_records(parentData, columns=parentCols)
  4.  
  5. Parent Dataframe
  6. PID PATTR1 PATTR2
  7. 0 1 A 100
  8. 1 2 B 200
  9.  
  10. childData = [(201,1,’AA’,2100), (202,2,’BB’,2200), (203,2,’CC’,2300)]
  11. childCols = [‘CID’, ‘PID’, ‘CATTR1’, ‘CATTR1’]
  12. childDf = pd.DataFrame.from_records(childData, columns=childCols)
  13.  
  14. Child Dataframe
  15. CID PID PATTR1 PATTR2
  16. 0 201 1 AA 2100
  17. 1 202 2 BB 2200
  18. 2 203 2 CC 2300
  19.  
  20. mergedDf = parentDf.merge(childDf, left_on=’PID’, right_on=’PID’, how=’outer’)
  21.  
  22. Parent merged with Child dataframe
  23. PID PATTR1 PATTR2 CID CATTR1 CATTR2
  24. 0 1 A 100 201 AA 2100
  25. 1 2 B 200 202 BB 2200
  26. 2 2 B 200 203 CC 2300
  27.  
  28. | ???? | ????
  29. PID PATTR1 PATTR2 | CID CATTR1 CATTR2 | CID CATTR1 CATTR2
  30. 0 1 A 100 | 201 AA 2100 |
  31. 1 2 B 200 | 202 BB 2200 | 203 CC 2300
  32.  
  33. mergedDf.assign(G=mergedDf.groupby('PID').cumcount()).set_index(['PID','PATTR1','PATTR2','G']).unstack().swaplevel(0,1,1).sort_index(1,level=0)
  34. Out[218]:
  35. G 0 1
  36. CATTR1 CATTR2 CID CATTR1 CATTR2 CID
  37. PID PATTR1 PATTR2
  38. 1 A 100 AA 2100.0 201.0 None NaN NaN
  39. 2 B 200 BB 2200.0 202.0 CC 2300.0 203.0
Add Comment
Please, Sign In to add comment