之前的所有范例都有着唯一的軸標簽(索引值)。
下面就看看帶有重復索引值的
Series:
1 obj=Series(range(5),index=['a','a','b','b','c']) 2 3 obj 4 Out[33]: 5 a 0 6 a 1 7 b 2 8 b 3 9 c 4 10 dtype: int64
索引的is_unique屬性可以告訴你它的值是否是唯一的:
obj.index.is_unique
Out[34]: False
對帶有重復值的索引,選取數據時,如果某個索引對應多個值,則返回一個Series;而對應單個值的,則返回一個標量值。
1 obj['a'] 2 Out[35]: 3 a 0 4 a 1 5 dtype: int64 6 7 obj['b'] 8 Out[36]: 9 b 2 10 b 3 11 dtype: int64
DataFrame的行索引也是如此:
1 df=DataFrame(np.random.randn(4,3),index=['a','a','b','b']) 2 3 df 4 Out[39]: 5 0 1 2 6 a -0.661187 -0.624006 0.073817 7 a -1.460339 0.705815 1.282448 8 b 1.759900 -0.149222 0.648127 9 b -1.236652 0.125667 -0.144872 10 11 df.ix['b'] 12 __main__:1: DeprecationWarning: 13 .ix is deprecated. Please use 14 .loc for label based indexing or 15 .iloc for positional indexing 16 17 See the documentation here: 18 http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated 19 Out[40]: 20 0 1 2 21 b 1.759900 -0.149222 0.648127 22 b -1.236652 0.125667 -0.144872