In [1]:
import pandas as pd
import numpy as np
In [3]:
d1 = pd.DataFrame(np.arange(12).reshape(3,4),index=list("ABC"),columns=list("WXYZ"))
print(d1)
 
   W  X   Y   Z
A  0  1   2   3
B  4  5   6   7
C  8  9  10  11
In [5]:
d1.index = ["O","P","Q"]
print(d1)
 
   W  X   Y   Z
O  0  1   2   3
P  4  5   6   7
Q  8  9  10  11
In [9]:
#使用reindex,和之前对不上的会被赋nan
d1.reindex(index=list("Opq"))
Out[9]:
 
 WXYZ
O 0.0 1.0 2.0 3.0
p NaN NaN NaN NaN
q NaN NaN NaN NaN
In [10]:
#将其中一列设为索引
d1.set_index("W")
Out[10]:
 
 XYZ
W   
0 1 2 3
4 5 6 7
8 9 10 11
In [11]:
d1.set_index("W",drop=False)
Out[11]:
 
 WXYZ
W    
0 0 1 2 3
4 4 5 6 7
8 8 9 10 11
In [13]:
d1.set_index(["W","X"],drop=False)
Out[13]:
 
  WXYZ
WX    
01 0 1 2 3
45 4 5 6 7
89 8 9 10 11
In [14]:
d1.set_index("W").index.unique()
Out[14]:
Int64Index([0, 4, 8], dtype='int64', name='W')
In [15]:
a = pd.DataFrame({'a': range(7),'b': range(7, 0, -1),'c': ['one','one','one','two','two','two', 'two'],'d': list("hjklmno")})
print(a)
 
   a  b    c  d
0  0  7  one  h
1  1  6  one  j
2  2  5  one  k
3  3  4  two  l
4  4  3  two  m
5  5  2  two  n
6  6  1  two  o
In [18]:
b = a.set_index(["d","c"])
print(b)
print(type(b))
 
       a  b
d c        
h one  0  7
j one  1  6
k one  2  5
l two  3  4
m two  4  3
n two  5  2
o two  6  1
<class 'pandas.core.frame.DataFrame'>
In [40]:
#想取b里的one索引
b = b.swaplevel()
print(b)
print("*"*20)
print(b.loc["one"])#DataFrame[""]形式只能用于取列,无法用来取行索引
print("*"*20)
print(b.loc["one"].loc["h"])
 
       a  b
c   d      
one h  0  7
    j  1  6
    k  2  5
two l  3  4
    m  4  3
    n  5  2
    o  6  1
********************
   a  b
d      
h  0  7
j  1  6
k  2  5
********************
a    0
b    7
Name: h, dtype: int64
In [41]:
c = b["a"]
print(type(c),"\n",c)
print("*"*20)
print(c["one","h"])#对于Series直接用[]取值就好了,不需要用loc
 
<class 'pandas.core.series.Series'> 
 c    d
one  h    0
     j    1
     k    2
two  l    3
     m    4
     n    5
     o    6
Name: a, dtype: int64
********************
0