Pandas loc/iloc用法詳解

2022-09-01 14:50 更新

在數(shù)據(jù)分析過程中,很多時候需要從數(shù)據(jù)表中提取出相應(yīng)的數(shù)據(jù),而這么做的前提是需要先“索引”出這一部分數(shù)據(jù)。雖然通過 Python 提供的索引操作符"[]"

和屬性操作符"."可以訪問 Series 或者 DataFrame 中的數(shù)據(jù),但這種方式只適應(yīng)與少量的數(shù)據(jù),為了解決這一問題,Pandas 提供了兩種類型的索引方式來實現(xiàn)數(shù)據(jù)的訪問。

本節(jié)就來講解一下,如何在 Pandas 中使用 loc 函數(shù)和 iloc 函數(shù)。兩種函數(shù)說明如下:

方法名稱 說明
.loc[] 基于標簽索引選取數(shù)據(jù)
.iloc[] 基于整數(shù)索引選取數(shù)據(jù)

.loc[]

df.loc[] 只能使用標簽索引,不能使用整數(shù)索引。當通過標簽索引的切片方式來篩選數(shù)據(jù)時,它的取值前閉后閉,也就是只包括邊界值標簽(開始和結(jié)束)。

.loc[] 具有多種訪問方法,如下所示:

  • 一個標量標簽
  • 標簽列表
  • 切片對象
  • 布爾數(shù)組

loc[] 接受兩個參數(shù),并以','分隔。第一個位置表示行,第二個位置表示列。示例如下:

import numpy as np
import pandas as pd
#創(chuàng)建一組數(shù)據(jù)
data = {'name': ['John', 'Mike', 'Mozla', 'Rose', 'David', 'Marry', 'Wansi', 'Sidy', 'Jack', 'Alic'],
        'age': [20, 32, 29, np.nan, 15, 28, 21, 30, 37, 25],
        'gender': [0, 0, 1, 1, 0, 1, 0, 0, 1, 1],
        'isMarried': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}
label = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(data, index=label)
print(df)
#對行操作
print(df.loc['a':'d',:]) #等同于df.loc['a':'d']

輸出結(jié)果:

    name   age  gender isMarried
a   John  20.0       0       yes
b   Mike  32.0       0       yes
c  Mozla  29.0       1        no
d   Rose   NaN       1       yes
e  David  15.0       0        no
f  Marry  28.0       1        no
g  Wansi  21.0       0        no
h   Sidy  30.0       0       yes
i   Jack  37.0       1        no
j   Alic  25.0       1        no
#從a到d,切記包含d
    name   age  gender isMarried
a   John  20.0       0       yes
b   Mike  32.0       0       yes
c  Mozla  29.0       1        no
d   Rose   NaN       1       yes

對列進行操作,示例如下:

import numpy as np
import pandas as pd
#創(chuàng)建一組數(shù)據(jù)
data = {'name': ['John', 'Mike', 'Mozla', 'Rose', 'David', 'Marry', 'Wansi', 'Sidy', 'Jack', 'Alic'],
        'age': [20, 32, 29, np.nan, 15, 28, 21, 30, 37, 25],
        'gender': [0, 0, 1, 1, 0, 1, 0, 0, 1, 1],
        'isMarried': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}
label = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(data, index=label)
print(df.loc[:,'name'])

輸出結(jié)果:

a     John
b     Mike
c    Mozla
d     Rose
e    David
f    Marry
g    Wansi
h     Sidy
i     Jack
j     Alic
Name: name, dtype: object

對行和列同時操作,示例如下:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(8, 4),
index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D'])
print(df.loc[['a','b','f','h'],['A','C']])

輸出如下:

          A         C
a  1.168658  0.008070
b -0.076196  0.455495
f  1.224038  1.234725
h  0.050292 -0.031327

布爾值操作,示例如下:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(4, 4),index = ['a','b','c','d'], columns = ['A', 'B', 'C', 'D'])
#返回一組布爾值
print(df.loc['b']>0)

輸出結(jié)果:

A     True
B     True
C    False
D     True
Name: b, dtype: bool

.iloc[]

df.iloc[] 只能使用整數(shù)索引,不能使用標簽索引,通過整數(shù)索引切片選擇數(shù)據(jù)時,前閉后開(不包含邊界結(jié)束值)。同 Python 和 NumPy 一樣,它們的索引都是從 0 開始。

.iloc[] 提供了以下方式來選擇數(shù)據(jù):

  • 1) 整數(shù)索引
  • 2) 整數(shù)列表
  • 3) 數(shù)值范圍

示例如下:

import pandas as pd
import numpy as np
data = {'name': ['John', 'Mike', 'Mozla', 'Rose', 'David', 'Marry', 'Wansi', 'Sidy', 'Jack', 'Alic'],
        'age': [20, 32, 29, np.nan, 15, 28, 21, 30, 37, 25],
        'gender': [0, 0, 1, 1, 0, 1, 0, 0, 1, 1],
        'isMarried': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}
label = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(data, index=label)
print(df)
print(df.iloc[2,])

輸出結(jié)果:

    name   age  gender isMarried
a   John  20.0       0       yes
b   Mike  32.0       0       yes
c  Mozla  29.0       1        no
d   Rose   NaN       1       yes
e  David  15.0       0        no
f  Marry  28.0       1        no
g  Wansi  21.0       0        no
h   Sidy  30.0       0       yes
i   Jack  37.0       1        no
j   Alic  25.0       1        no

name         Mozla
age             29
gender           1
isMarried       no
Name: c, dtype: object

再看一組示例:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D'])
print df.iloc[[1, 3, 5], [1, 3]]
print df.iloc[1:3, :]
print df.iloc[:,1:3]

輸出結(jié)果:

          B         D
1  0.773595 -0.206061
3 -1.740403 -0.464383
5  1.046009  0.606808

          A         B         C         D
1 -0.093711  0.773595  0.966408 -0.206061
2 -1.122587 -0.135011  0.546475 -0.551403

          B         C
0  0.623488  3.328406
1  0.773595  0.966408
2 -0.135011  0.546475
3 -1.740403 -0.869073
4  0.591573 -1.463275
5  1.046009  2.330035
6 -0.266607  0.873971
7 -1.059625 -0.405340


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號