pandas的索引操作-創(chuàng)新互聯(lián)

Pandas的索引操作

索引對象Index

1. Series和DataFrame中的索引都是Index對象

示例代碼:

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了和田縣免費建站歡迎大家使用!
print(type(ser_obj.index))
print(type(df_obj2.index))

print(df_obj2.index)

運行結(jié)果:

<class 'pandas.indexes.range.RangeIndex'>
<class 'pandas.indexes.numeric.Int64Index'>
Int64Index([0, 1, 2, 3], dtype='int64')

2. 索引對象不可變,保證了數(shù)據(jù)的安全

示例代碼:

# 索引對象不可變
df_obj2.index[0] = 2

運行結(jié)果:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-7f40a356d7d1> in <module>()
      1 # 索引對象不可變
----> 2 df_obj2.index[0] = 2

/Users/Power/anaconda/lib/python3.6/site-packages/pandas/indexes/base.py in __setitem__(self, key, value)
   1402 
   1403     def __setitem__(self, key, value):
-> 1404         raise TypeError("Index does not support mutable operations")
   1405 
   1406     def __getitem__(self, key):

TypeError: Index does not support mutable operations

常見的Index種類

  • Index, 索引
  • Int64Index, 整數(shù)索引
  • MultiIndex, 層級索引
  • DatatimeIndex, 時間戳類型

Series索引

1. index指定行索引名

示例代碼:

ser_obj = pd.Series(range(5), index = ['a', 'b', 'c', 'd', 'e'])
print(ser_obj.head())

運行結(jié)果:

a    0
b    1
c    2
d    3
e    4
dtype: int64

2. 行索引

ser_obj['label'], ser_obj[pos]

示例代碼:

print(ser_obj['b'])
print(ser_obj[2])

運行結(jié)果:

1
2

3. 切片索引

ser_obj[2:4], ser_obj['label1':'label3']

注意, 按索引名切片操作時,時包含終止索引的

示例代碼:

print(ser_obj[1:3])
print(ser_obj['b':'d'])

運行結(jié)果:

b    1
c    2
dtype: int64
b    1
c    2
d    3
dtype: int64

4. 不連續(xù)索引

ser_obj[['label1', 'label2', 'label3']]

示例代碼:

print(ser_obj[[0, 2, 4]])
print(ser_obj[['a', 'e']])

運行結(jié)果:

a    0
c    2
e    4
dtype: int64
a    0
e    4
dtype: int64

5. 布爾索引

示例代碼:

ser_bool = ser_obj > 2
print(ser_bool)
print(ser_obj[ser_bool])

print(ser_obj[ser_obj > 2])

運行結(jié)果:

a    False
b    False
c    False
d     True
e     True
dtype: bool
d    3
e    4
dtype: int64
d    3
e    4
dtype: int64

DataFrame索引

1. columns指定列索引名

示例代碼:

import numpy as np

df_obj = pd.DataFrame(np.random.randn(5, 4), columns = ['a', 'b', 'c', 'd'])
print(df_obj.head())

運行結(jié)果:

   a         b         c         d
0 -0.241678  0.621589  0.843546 -0.383105
1 -0.526918 -0.485325  1.124420 -0.653144
2 -1.074163  0.939324 -0.309822 -0.209149
3 -0.716816  1.844654 -2.123637 -1.323484
4  0.368212 -0.910324  0.064703  0.486016
  • DataFrame索引

                                   Colum index (df.columns)
       Row index(df.index)           a         b         c         d
                                    0 -0.241678  0.621589  0.843546 -0.383105
                                    1 -0.526918 -0.485325  1.124420 -0.653144
                                    2 -1.074163  0.939324 -0.309822 -0.209149
                                    3 -0.716816  1.844654 -2.123637 -1.323484
                                    4  0.368212 -0.910324  0.064703  0.486016

2. 列索引

df_obj[['label']]

示例代碼:

print(df_obj['a']) # 返回Series類型
print(df_obj[[0]])# 返回DataFrame類型,ipython3中不支持
print(type(df_obj[[0]])) # 返回DataFrame類型,ipython3中不支持

運行結(jié)果:

0   -0.241678
1   -0.526918
2   -1.074163
3   -0.716816
4    0.368212
Name: a, dtype: float64
<class 'pandas.core.frame.DataFrame'>

3. 不連續(xù)索引

df_obj[['label1', 'label2']]

示例代碼:

print(df_obj[['a', 'c']])
print(df_obj[[1, 3]]) # ipython3中不支持

運行結(jié)果:

   a         c
0 -0.241678  0.843546
1 -0.526918  1.124420
2 -1.074163 -0.309822
3 -0.716816 -2.123637
4  0.368212  0.064703
          b         d
0  0.621589 -0.383105
1 -0.485325 -0.653144
2  0.939324 -0.209149
3  1.844654 -1.323484
4 -0.910324  0.486016

高級索引:標簽、位置和混合

Pandas的高級索引有3種

1. loc標簽索引
DataFrame不能直接切片,可以通過loc來做切片

loc是基于標簽名的索引,也就是我們自定義的索引名

示例代碼:

# 標簽索引 loc
# Series
print(ser_obj['b':'d'])
print(ser_obj.loc['b':'d'])

# DataFrame
print(df_obj['a'])

# 第一個參數(shù)索引行,第二個參數(shù)是列
print(df_obj.loc[0:2, 'a'])

運行結(jié)果:

b    1
c    2
d    3
dtype: int64
b    1
c    2
d    3
dtype: int64

0   -0.241678
1   -0.526918
2   -1.074163
3   -0.716816
4    0.368212
Name: a, dtype: float64
0   -0.241678
1   -0.526918
2   -1.074163
Name: a, dtype: float64
2. iloc位置索引
作用和loc一樣,不過是基于索引編號來索引

示例代碼:

# 整型位置索引 iloc
# Series
print(ser_obj[1:3])
print(ser_obj.iloc[1:3])

# DataFrame
print(df_obj.iloc[0:2, 0]) # 注意和df_obj.loc[0:2, 'a']的區(qū)別

運行結(jié)果:

b    1
c    2
dtype: int64
b    1
c    2
dtype: int64

0   -0.241678
1   -0.526918
Name: a, dtype: float64
3. ix標簽與位置混合索引
ix是以上二者的綜合,既可以使用索引編號,又可以使用自定義索引,要視情況不同來使用,

如果索引既有數(shù)字又有英文,那么這種方式是不建議使用的,容易導致定位的混亂。

示例代碼:

# 混合索引 ix
# Series
print(ser_obj.ix[1:3])
print(ser_obj.ix['b':'c'])

#DataFrame
print(df_obj.loc[0:2, 'a'])
print(df_obj.ix[0:2, 0])

運行結(jié)果

b    1
c    2
dtype: int64
b    1
c    2
dtype: int64

0   -0.241678
1   -0.526918
2   -1.074163
Name: a, dtype: float64

注意

DataFrame索引操作,可將其看作ndarray的索引操作

標簽的切片索引是包含末尾位置的

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站標題:pandas的索引操作-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://www.muchs.cn/article4/depcie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、企業(yè)網(wǎng)站制作、手機網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站設(shè)計軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)