欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中pandas庫(kù)的iloc函數(shù)用法解析

 更新時(shí)間:2023年05月09日 10:12:46   作者:bksheng  
在 Pandas 中,.iloc 是一種用于基于整數(shù)位置進(jìn)行索引的屬性,可以用于獲取 DataFrame 或 Series 中的數(shù)據(jù),這篇文章主要介紹了python中pandas庫(kù)的iloc函數(shù)用法,需要的朋友可以參考下

python中pandas庫(kù)的iloc函數(shù)用法

在 Pandas 中,.iloc 是一種用于基于整數(shù)位置進(jìn)行索引的屬性,可以用于獲取 DataFrame 或 Series 中的數(shù)據(jù)。.iloc 支持多種索引方式,包括以下常用方式:

1. 單個(gè)整數(shù)位置索引

使用整數(shù)索引獲取 DataFrame 或 Series 中的單個(gè)元素。
例如 df.iloc[0, 1] 表示獲取 DataFrame 中第一行第二列的數(shù)據(jù)。

2. 整數(shù)位置范圍索引

使用整數(shù)索引獲取 DataFrame 或 Series 中的多個(gè)元素。
例如 df.iloc[0:3, 1:3] 表示獲取 DataFrame 中第一行到第三行、第二列到第四列的數(shù)據(jù)。

3. 整數(shù)位置列表索引

使用整數(shù)列表索引獲取 DataFrame 或 Series 中的多個(gè)元素。
例如 df.iloc[[0, 2, 4], [1, 3, 5]] 表示獲取 DataFrame 中第一行、第三行、第五行和第二列、第四列、第六列的數(shù)據(jù)。

4. 布爾值索引

使用布爾值索引獲取 DataFrame 或 Series 中的多個(gè)元素。
例如 df.iloc[df["col1"] > 0, [1, 3, 5]] 表示獲取 DataFrame 中 col1 列大于 0 的行的第二列、第四列、第六列的數(shù)據(jù)。

注意:.iloc 屬性基于整數(shù)(數(shù)字索引)位置進(jìn)行索引,如果需要基于標(biāo)簽(標(biāo)簽列名)進(jìn)行索引,應(yīng)該使用 .loc 屬性。

補(bǔ)充:python中iloc與loc的區(qū)別

loc和iloc都是pandas工具中定位某一行的函數(shù),loc是location的意思,而iloc中的 i 指的是Integer,二者的區(qū)別如下:

  • loc:通過(guò)行標(biāo)簽名稱索引行數(shù)據(jù)
  • iloc:通過(guò)行號(hào)索引行數(shù)據(jù) 示例數(shù)據(jù)
import numpy as np
import pandas as pd
data=DataFrame(np.arange(16).reshape(4,4),index=list("ABCD"),columns=list("wxyz"))
print(data)

輸出如下:

    w   x   y   z
A   0   1   2   3
B   4   5   6   7
C   8   9  10  11
D  12  13  14  15

loc用法

print(data.loc["A"])
#w    0
#x    1
#y    2
#z    3
print(data.loc[["A"]])
#   w  x  y  z
#A  0  1  2  3
# []返回Series,[[]]返回DataFrame

iloc用法

print(data.loc["A"])
#w    0
#x    1
#y    2
#z    3
print(data.loc[["A"]])
#   w  x  y  z
#A  0  1  2  3
# []返回Series,[[]]返回DataFrame

到此這篇關(guān)于python中pandas庫(kù)的iloc函數(shù)用法的文章就介紹到這了,更多相關(guān)python pandas庫(kù)iloc函數(shù)用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論