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

一文秒懂pandas中iloc()函數(shù)

 更新時(shí)間:2024年07月26日 09:56:47   作者:sci_more  
iloc[]函數(shù)屬于pandas庫全稱為index?location,即對(duì)數(shù)據(jù)進(jìn)行位置索引,從而在數(shù)據(jù)表中提取出相應(yīng)的數(shù)據(jù),本文通過實(shí)例代碼介紹pandas中iloc()函數(shù),感興趣的朋友一起看看吧

pandas中iloc()函數(shù)

DataFrame.iloc
純基于整數(shù)位置的索引。

import pandas as pd
mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
           {'a': 100, 'b': 200, 'c': 300, 'd': 400},
           {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]
'''mydict
[{'a': 1, 'b': 2, 'c': 3, 'd': 4},
 {'a': 100, 'b': 200, 'c': 300, 'd': 400},
 {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000}]'''
df = pd.DataFrame(mydict)
'''
df
	a	b	c	d
0	1	2	3	4
1	100	200	300	400
2	1000	2000	3000	4000
'''

??¨è??é????'?¥????‰????è?°

df.iloc[0]#取第0行
a    1
b    2
c    3
d    4
Name: 0, dtype: int64
df.iloc[0].shape
(4,)
type(df.iloc[0].shape)
tuple
df.iloc[[0]]
	a	b	c	d
0	1	2	3	4
type(df.iloc[[0]])
pandas.core.frame.DataFrame
df.iloc[[0,2]]#取第0、2行
	   a	   b	   c	   d
0	   1	   2	   3	   4
2	1000	2000	3000	4000
df.iloc[0:2,0:3]#取0到1行和0到2列
	  a	  b   c
0	  1	  2	  3
1	100	200	300
df.iloc[[True, False, True]]#不常用
	   a	   b	   c	   d
0	   1	   2	   3	   4
2	1000	2000	3000	4000
df.iloc[lambda x: x.index % 2 == 0]#函數(shù)生成索引列表,x即df
       a	   b	   c	   d
0	   1	   2	   3	   4
2	1000	2000	3000	4000

Pandas庫中iloc[ ]函數(shù)使用詳解

1 iloc[]函數(shù)作用

iloc[]函數(shù),屬于pandas庫,全稱為index location,即對(duì)數(shù)據(jù)進(jìn)行位置索引,從而在數(shù)據(jù)表中提取出相應(yīng)的數(shù)據(jù)。

2 iloc函數(shù)使用

df.iloc[a,b],其中df是DataFrame數(shù)據(jù)結(jié)構(gòu)的數(shù)據(jù)(表1就是df),a是行索引(見表1),b是列索引(見表1)。

姓名(列索引10)班級(jí)(列索引1)分?jǐn)?shù)(列索引2)
0(行索引0)小明30287
1(行索引1)小王30395
2(行索引2)小方303100

1.iloc[a,b]:取行索引為a列索引為b的數(shù)據(jù)。

import pandas
df = pandas.read_csv('a.csv')
print(df.iloc[1,2])
#Out:95

2.iloc[a:b,c]:取行索引從a到b-1,列索引為c的數(shù)據(jù)。注意:在iloc中a:b是左到右不到的,即lioc[1:3,:]是從行索引從1到2,所有列索引的數(shù)據(jù)。

import pandas
df = pandas.read_csv('a.csv')
print(df.iloc[0:2,2]) #數(shù)據(jù)結(jié)構(gòu)是Series
print(df.iloc[0:2,2].values) #數(shù)據(jù)結(jié)構(gòu)是ndarray
#Out1:0    87
#      1    95
# Name: 分?jǐn)?shù), dtype: int64
#Out2:[87 95]

iloc[].values,用values屬性取值,返回ndarray,但是單個(gè)數(shù)值無法用values函數(shù)讀取。 

 3.iloc[a:b,c:d]:取行索引從a到b-1,列索引從c到d-1的數(shù)據(jù)。

import pandas
df = pandas.read_csv('a.csv')
print(df.iloc[0:2,0:2])
print(df.iloc[0:2,0:2].values)
#Out1:   姓名   班級(jí)
#      0  小明  302
#      1  小王  303
#Out2:[['小明' 302]
#       ['小王' 303]]

4.iloc[a]:取取行索引為a,所有列索引的數(shù)據(jù)。

import pandas
df = pandas.read_csv('a.csv')
print(df.iloc[2])
print(df.iloc[2].values)
#Out1:姓名     小方
#      班級(jí)    303
#      分?jǐn)?shù)    100
# Name: 2, dtype: object
#Out2:['小方' 303 100]

補(bǔ)充:pandas的iloc函數(shù)

pandas的iloc函數(shù):

iloc 是 Pandas 中用于基于整數(shù)位置進(jìn)行索引和切片的方法。它允許你通過整數(shù)位置來訪問 DataFrame 中的特定行和列。

語法格式如下:

DataFrame.iloc[row_indexer, column_indexer]
  • row_indexer: 行的整數(shù)位置或切片。
  • column_indexer: 列的整數(shù)位置或切片。

下面是一些使用 iloc 的示例:

import pandas as pd
# 創(chuàng)建一個(gè)示例 DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40],
        'City': ['New York', 'San Francisco', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
# 使用 iloc 獲取特定行和列的數(shù)據(jù)
# 獲取第二行(索引為1)的所有列數(shù)據(jù)
row_1 = df.iloc[1, :]
# 獲取第一列(索引為0)的所有行數(shù)據(jù)
column_0 = df.iloc[:, 0]
# 獲取第二行到第四行(索引為1到3)的第一列和第二列的數(shù)據(jù)
subset = df.iloc[1:4, 0:2]
print("Row 1:")
print(row_1)
print("\nColumn 0:")
print(column_0)
print("\nSubset:")
print(subset)

在這個(gè)例子中,iloc 被用于獲取指定的行和列。要注意,iloc 使用的是整數(shù)位置,而不是標(biāo)簽。索引從0開始。這使得 iloc 適用于對(duì) DataFrame 進(jìn)行基于位置的切片和索引。

Row 1:
Name              Bob
Age                30
City    San Francisco
Name: 1, dtype: object
Column 0:
0      Alice
1        Bob
2    Charlie
3      David
Name: Name, dtype: object
Subset:
      Name  Age
1      Bob   30
2  Charlie   35
3    David   40

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

相關(guān)文章

最新評(píng)論