Guest User

Untitled

a guest
Jan 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. pd.options.display.notebook_repr_html = False
  4.  
  5. # hierarchical indices and columns
  6. index = pd.MultiIndex.from_product([[2014,2016,2018], [1, 2, 3]],
  7. names=['year', 'sample_num'])
  8. columns = pd.MultiIndex.from_product([['Kinshati', 'Ryujinboku', 'Ranpougyoku'], ['width', 'Height']],
  9. names=['Cactus', 'length'])
  10. # mock some data
  11. data = np.round(np.random.randn(9, 6), 1)
  12. data[:, ::2] *= 5
  13. data += 10
  14.  
  15. # create the DataFrame
  16. cactus_data = pd.DataFrame(data, index=index, columns=columns)
  17. cactus_data
  18. """
  19. Cactus Kinshati Ryujinboku Ranpougyoku
  20. length width Height width Height width Height
  21. year sample_num
  22. 2014 1 11.5 10.4 15.5 9.3 18.5 8.7
  23. 2 20.0 9.8 10.5 10.2 20.0 10.5
  24. 3 11.5 10.4 18.5 11.1 6.0 9.7
  25. 2016 1 5.5 9.3 13.0 9.1 6.5 8.4
  26. 2 5.5 9.1 6.0 9.5 10.5 10.1
  27. 3 11.5 10.9 6.0 9.0 10.0 9.0
  28. 2018 1 17.5 9.1 14.5 10.7 13.0 10.6
  29. 2 9.5 10.4 1.5 10.6 13.5 10.6
  30. 3 4.5 9.7 12.5 8.9 9.5 9.8
  31. """_
  32.  
  33. data_heikin = cactus_data.mean(level='year')
  34. data_heikin
  35. """
  36. Cactus Kinshati Ryujinboku Ranpougyoku
  37. length width Height width Height width Height
  38. year
  39. 2014 14.333333 10.200000 14.833333 10.200000 14.833333 9.633333
  40. 2016 7.500000 9.766667 8.333333 9.200000 9.000000 9.166667
  41. 2018 10.500000 9.733333 9.500000 10.066667 12.000000 10.333333
  42. """
  43.  
  44. data_heikin2 = cactus_data.mean(axis=1,level='length')
  45. data_heikin2
  46. """
  47. length width Height
  48. year sample_num
  49. 2014 1 15.166667 9.466667
  50. 2 16.833333 10.166667
  51. 3 12.000000 10.400000
  52. 2016 1 8.333333 8.933333
  53. 2 7.333333 9.566667
  54. 3 9.166667 9.633333
  55. 2018 1 15.000000 10.133333
  56. 2 8.166667 10.533333
  57. 3 8.833333 9.466667
  58. """
Add Comment
Please, Sign In to add comment