. 1 Import PANDAS AS PD 2 Import numpy AS NP . 3 . 4 S = pd.Series (np.random.rand (. 5), = List index ( ' ABCDE ' )) . 5 # create a sequence, wherein the List = index ( ' ABCDE ' ) adding an index for each row . 6 s.index.name = ' Alpha ' # name tagged line index . 7 . 8 DF = pd.DataFrame (np.random.randn (4,3-), Columns = [ ' One ' , ' TWO ' , ' Three ' ]) . 9 #Create DataFrame, where columns = [ 'one', ' two', 'three'] denotes an index for each column add 10 df.index.name = ' Row ' # row index to be tagged . 11 df.columns.name = ' COL ' # add name tags for the column index
. 1 Import PANDAS AS PD 2 Import numpy AS NP . 3 . 4 S = pd.Series (np.arange (. 6), index = List ( ' abcbda ' )) . 5 # Create a duplicate index with Series . 6 . 7 S [ ' A ' ] # to find out a value corresponding to the index for all . 8 s.index.is_unique # determines whether unique index for each s . 9 s.index.unique () # find no duplicate index s 10 . 11 s.groupby ( s.index) .sum () # index grouped and summed 12 is s.groupby (s.index) .mean () # index packets and averaging 13s.groupby (s.index) .first () # index and take the first packet
1 import pandas as pd 2 import numpy as np 3 4 a = [['a','a','a','b','b','c','c'],[1,2,3,1,2,2,3]] 5 t = list(zip(*a)) 6 index = pd.MultiIndex.from_tuples(t,names=['level1','level2']) . 7 S = pd.Series (np.random.rand (. 7), index = index) . 8 # output S . 9 Level-1 Level2 10 A 0.029233. 1 . 11 2 .539508 12 is . 3 0.502217 13 is B. 1 0.536222 14 2 0.217398 15 C 0.551864 2 16 . 3 .596248 . 17 18 is S [ ' B ' ] . 19 # output 20 is Level2 21 is . 1 .536222 22 is 2 0.217398 23 is DTYPE: float64 24 25 S [ ' B ' : ' C ' ] 26 is # output 27 Level-1 Level2 28 B. 1 .536222 29 2 .217398 30 C 2 0.551864 31 is . 3 .596248 32 DTYPE: float64 33 is 34 is S [[ ' A ' , ' C ' ]] 35 # outputs 36 Level-1 Level2 37 [ a 1 0.029233 38 2 0.539508 39 3 0.502217 40 c 2 0.551864 41 3 0.596248 42 dtype: float64 43 44 s[:,2] 45 # 输出 46 level1 47 a 0.539508 48 b 0.217398 49 c 0.551864 50 dtype: float64