您好,登錄后才能下訂單哦!
iloc
是 pandas 庫(kù)中的一個(gè)功能,它允許我們基于整數(shù)索引來選擇、過濾和操作 DataFrame 或 Series 的數(shù)據(jù)
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)
iloc
獲取第 1 行(位置為 0 的行)的數(shù)據(jù):first_row = df.iloc[0]
iloc
獲取前兩列(位置為 0 和 1 的列)的數(shù)據(jù):first_two_columns = df.iloc[:, :2]
iloc
獲取第 1 到第 3 行(位置為 0 到 2 的行)的數(shù)據(jù):first_three_rows = df.iloc[0:3]
iloc
獲取第 2 列(位置為 1 的列)的數(shù)據(jù):second_column = df.iloc[:, 1]
iloc
修改第 1 行第 2 列(位置為 0,1)的值:df.iloc[0, 1] = 99
iloc
添加一行數(shù)據(jù):new_row = pd.Series([10, 11, 12], index=df.columns, name='D')
df = df.append(new_row)
iloc
刪除一行數(shù)據(jù):df = df.drop(df.index[0])
iloc
對(duì)數(shù)據(jù)進(jìn)行條件篩選:filtered_data = df[df.iloc[:, 1] > 5]
通過以上示例,您可以了解如何使用 iloc
進(jìn)行高效的數(shù)據(jù)處理。請(qǐng)注意,iloc
主要針對(duì)基于整數(shù)位置的索引,而不是基于標(biāo)簽的索引。如果您需要基于標(biāo)簽進(jìn)行索引,可以使用 loc
方法。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。