Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- pd.options.display.notebook_repr_html = False
- # hierarchical indices and columns
- index = pd.MultiIndex.from_product([[2014,2016,2018], [1, 2, 3]],
- names=['year', 'sample_num'])
- columns = pd.MultiIndex.from_product([['Kinshati', 'Ryujinboku', 'Ranpougyoku'], ['width', 'Height']],
- names=['Cactus', 'length'])
- # mock some data
- data = np.round(np.random.randn(9, 6), 1)
- data[:, ::2] *= 5
- data += 10
- # create the DataFrame
- cactus_data = pd.DataFrame(data, index=index, columns=columns)
- cactus_data
- """
- Cactus Kinshati Ryujinboku Ranpougyoku
- length width Height width Height width Height
- year sample_num
- 2014 1 11.5 10.4 15.5 9.3 18.5 8.7
- 2 20.0 9.8 10.5 10.2 20.0 10.5
- 3 11.5 10.4 18.5 11.1 6.0 9.7
- 2016 1 5.5 9.3 13.0 9.1 6.5 8.4
- 2 5.5 9.1 6.0 9.5 10.5 10.1
- 3 11.5 10.9 6.0 9.0 10.0 9.0
- 2018 1 17.5 9.1 14.5 10.7 13.0 10.6
- 2 9.5 10.4 1.5 10.6 13.5 10.6
- 3 4.5 9.7 12.5 8.9 9.5 9.8
- """_
- data_heikin = cactus_data.mean(level='year')
- data_heikin
- """
- Cactus Kinshati Ryujinboku Ranpougyoku
- length width Height width Height width Height
- year
- 2014 14.333333 10.200000 14.833333 10.200000 14.833333 9.633333
- 2016 7.500000 9.766667 8.333333 9.200000 9.000000 9.166667
- 2018 10.500000 9.733333 9.500000 10.066667 12.000000 10.333333
- """
- data_heikin2 = cactus_data.mean(axis=1,level='length')
- data_heikin2
- """
- length width Height
- year sample_num
- 2014 1 15.166667 9.466667
- 2 16.833333 10.166667
- 3 12.000000 10.400000
- 2016 1 8.333333 8.933333
- 2 7.333333 9.566667
- 3 9.166667 9.633333
- 2018 1 15.000000 10.133333
- 2 8.166667 10.533333
- 3 8.833333 9.466667
- """
Add Comment
Please, Sign In to add comment