pandas 帶有重複值的軸索引

2022-08-30 03:51:08 字數 1494 閱讀 8087

之前的所有範例都有著唯一的軸標籤(索引值)。

下面就看看帶有重複索引值的series

1 obj=series(range(5),index=['

a','

a','

b','

b','c'

])23obj

4 out[33]:

5a 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]:

3a 0

4 a 1

5dtype: int64

67 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'

])23df

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

1011 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

1617

see the documentation here:

18 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

pandas統計重複值次數

from pandas import dataframe df dataframe print df key1 key2 key3 0 a one 1 1 a two 2 2 b one 3 3 b two 2 4 a one 1 5 a one 1 6 b two 2 7 b two 3 重複項 ...

pandas 處理異常值缺失值重複值資料差分

處理異常值缺失值重複值資料差分 import pandas as pd import numpy as np import copy 設定列對齊 pd.set option display.unicode.ambiguous as wide true pd.set option display.un...

Pandas中loc用法 索引 補全缺失值

loc 通過行標籤索引行資料 loc 1 表示索引的是第1行 index 是整數 import pandas as pd data 1,2,3 4,5,6 index 0,1 columns a b c df pd.dataframe data data,index index,columns co...