Pandas hierarchical index Library 07_

import numpy as np
import pandas as pd

= {T_DATA
"name": [ "Don Ho", "Wang", "Pharaoh", "Zhao Three", "John Doe", "Wang sister"],
"Sex": [ "male", "female "," male "," female "," male "," female "],
" year ": [37,22,15,18,33,25],
" City ": [" Chengdu "," Beijing " "Shanghai", "Chengdu", "Shenzhen", "Beijing"]
}

# pd.Series objects
# pds1 = pd.Series ([1,2,3,4,5,6,7,8,9] , index = [[ "X", "X", "X", "Y "," Y "," Y "," Z "," Z "," Z "], [" a "," b "," c "," a "," b "," c "," a "," B "," C "]])
# Print (PDS1) # i.e., a two-dimensional array or list index tab with
" ""
X-a. 1
B 2
C. 3
the Y. 4 a
B. 5
C. 6
the Z. 7 a
B . 8
C. 9
"" "
# Print (PDS1 [" X-"] [" B "]) # access, the previous layer [] [] after the layer
# print (pds1 [" X " ," c "]) # access , [previous layer, after the layer]
# print (pds1 [:, " b"]) # outermost available slice
# # print (pds1 [:] [ "b"]) # slice is not a first layer thus obtained
# print (pds1 [ "Y"]) # The second layer section is not available, but as long as the outermost layer is given, the default is the entire second layer
# print (pds1 [ "Y" ] [0: 2]) # slice of the second layer thus obtained may
# Print (pds1.index)
"" "
MultiIndex ([( 'X-', 'A'),
( 'X-', 'B'),
( 'X-', 'C'),
( 'the Y', 'A') ,
('Y', 'b'),
('Y', 'c'),
('Z', 'a'),
('Z', 'b'),
('Z', 'c')],
)
"""

#DataFrame objects
# pddf1 = pd.DataFrame (t_data, index = [[ "A", "A", "A", "B", "B", "B"], [1,2,3,1, 2, 3]])
# Print (pddf1)
"" "
name Sex year City
A 1 37 Chengdu Tang Hao male
2 female 22 Beijing Wang
3 Pharaoh male 15 Shanghai
B 1 Zhao Chengdu and three women 18
2 33 John Doe Shenzhen male
3 female Wang sister Beijing 25
"" "

pddf2=pd.DataFrame(np.arange(10,26).reshape(4,4),index=[["A","A","B","B"],[1,2,1,2]],columns=[["左二","左二","右二","右二"],["a","b","a","b"]])
print(pddf2)
"""
左二 右二
a b a b
A 1 10 11 12 13
2 14 15 16 17
B 1 18 19 20 21
2 22 23 24 25
"""

Guess you like

Origin www.cnblogs.com/yiyea/p/11441809.html