import pandas as pd
import numpy as np
d1 = pd.DataFrame(np.arange(12).reshape(3,4),index=list("ABC"),columns=list("WXYZ"))
print(d1)
d1.index = ["O","P","Q"]
print(d1)
#使用reindex,和以前對不上的會被賦nan
d1.reindex(index=list("Opq"))
#將其中一列設爲索引
d1.set_index("W")
d1.set_index("W",drop=False)
d1.set_index(["W","X"],drop=False)
d1.set_index("W").index.unique()
a = pd.DataFrame({'a': range(7),'b': range(7, 0, -1),'c': ['one','one','one','two','two','two', 'two'],'d': list("hjklmno")})
print(a)
b = a.set_index(["d","c"])
print(b)
print(type(b))
#想取b裏的one索引
b = b.swaplevel()
print(b)
print("*"*20)
print(b.loc["one"])#DataFrame[""]形式只能用於取列,沒法用來取行索引
print("*"*20)
print(b.loc["one"].loc["h"])
c = b["a"]
print(type(c),"\n",c)
print("*"*20)
print(c["one","h"])#對於Series直接用[]取值就行了,不須要用loc