Guest User

Untitled

a guest
Jun 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. import numpy
  2. import pandas
  3.  
  4. numpy.random.seed(0)
  5. index1 = pandas.MultiIndex.from_product(
  6. [['A'], ['b'], ['one', 'two']],
  7. names=['City', 'Street', 'House']
  8. )
  9.  
  10. index2 = pandas.MultiIndex.from_product(
  11. [['C'], ['three', 'four'], ['d']],
  12. names=['City', 'House', 'Street']
  13. )
  14.  
  15. df1 = pandas.DataFrame(
  16. [['one-c1', 'one-c2'], ['two-c1', 'two-c2']],
  17. index=index1,
  18. columns=['c1', 'c2']
  19. )
  20. print(df1)
  21. df2 = pandas.DataFrame(
  22. [['three-c2', 'three-c1'], ['four-c2', 'four-c1']],
  23. index=index2,
  24. columns=['c2', 'c1']
  25. )
  26. print(df2)
  27. print(pandas.concat([df1, df2]))
  28.  
  29.  
  30. ### the work-around
  31. index_names = df1.index.names
  32. df12 = pandas.concat([df1.reset_index(), df2.reset_index()]).set_index(index_names)
Add Comment
Please, Sign In to add comment