您好,登錄后才能下訂單哦!
iloc
是 pandas 庫(kù)中的一個(gè)重要功能,它提供了基于整數(shù)索引的數(shù)據(jù)選擇方式
以下是 iloc
的一些常見(jiàn)用法:
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
# 選擇第一行
first_row = df.iloc[0]
print(first_row)
# 選擇前兩行
first_two_rows = df.iloc[:2]
print(first_two_rows)
# 選擇列 A
column_a = df.iloc[:, 0]
print(column_a)
# 選擇列 A 和 B
columns_a_and_b = df.iloc[:, :2]
print(columns_a_and_b)
# 選擇第一行和第二列
element_1_2 = df.iloc[0, 1]
print(element_1_2)
# 選擇大于 2 的元素
greater_than_2 = df.iloc[(df > 2).values]
print(greater_than_2)
iloc
進(jìn)行數(shù)據(jù)篩選:# 篩選出列 A 大于 1 的行
filtered_data = df.iloc[df['A'] > 1]
print(filtered_data)
iloc
進(jìn)行數(shù)據(jù)排序:# 按列 A 降序排序
sorted_data = df.iloc[df['A'].argsort()[::-1]]
print(sorted_data)
總之,iloc
是 pandas 中非常實(shí)用的數(shù)據(jù)選擇方法,可以幫助我們更方便地處理數(shù)據(jù)。
免責(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)容。