What is the difference between x [1,2] and x [1] [2] in indexing the hierarchy for series in python?

I have a series

x=pd.Series(np.random.random(16),index=[[1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4],['a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d']]) 

as follows:

1  a   -0.068167
   b   -1.036551
   c   -0.246619
   d    1.318381
2  a   -0.119061
   b    0.249653
   c    0.819153
   d    1.334510
3  a    0.029305
   b   -0.879798
   c    1.081574
   d   -1.590322
4  a    0.620149
   b   -2.197523
   c    0.927573
   d   -0.274370
dtype: float64

What is the difference between x [1, 'a'] and x [1] ['a']. This gives me the same answer. I am confused, what is the inner difference? When should you use the above two indexes?

+4
source share
2 answers

This explanation is from the Numpy docs , however I believe a similar thing happens in pandas (which uses NumPy internally using "indexers" to provide a mapping between the (possibly) named index and the base index based on integers).

, , x [0,2] = x [0] [2], , 2.

; 30 :

In [79]: %timeit x[1, 'a']
100000 loops, best of 3: 8.46 µs per loop

In [80]: %timeit x[1]['a']
1000 loops, best of 3: 274 µs per loop
+2

x[1, 'a'] pandas 1, 'a' (1, 'a') , (1, 'a').

x[1]['a'], pandas , , [], , , , , , . x[1] x, ['a'].

+1

Source: https://habr.com/ru/post/1649725/


All Articles