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)
In [5]:
d1.index = ["O","P","Q"]
print(d1)
In [9]:
#使用reindex,和之前对不上的会被赋nan
d1.reindex(index=list("Opq"))
Out[9]:
In [10]:
#将其中一列设为索引
d1.set_index("W")
Out[10]:
In [11]:
d1.set_index("W",drop=False)
Out[11]:
In [13]:
d1.set_index(["W","X"],drop=False)
Out[13]:
In [14]:
d1.set_index("W").index.unique()
Out[14]:
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)
In [18]:
b = a.set_index(["d","c"])
print(b)
print(type(b))
In [40]:
#想取b里的one索引
b = b.swaplevel()
print(b)
print("*"*20)
print(b.loc["one"])#DataFrame[""]形式只能用于取列,无法用来取行索引
print("*"*20)
print(b.loc["one"].loc["h"])
In [41]:
c = b["a"]
print(type(c),"
",c)
print("*"*20)
print(c["one","h"])#对于Series直接用[]取值就好了,不需要用loc