禁止拿我們的網(wǎng)站做宣傳市場(chǎng)調(diào)研報(bào)告3000字范文
文章目錄
- `df.iloc` 常見用法
- 1. 獲取特定行
- 2. 獲取特定列
- 3. 獲取特定的行和列
- 4. 獲取行切片
- 5. 獲取列切片
- 6. 獲取特定的行和列切片
- `df.loc` 常見用法
- 1. 獲取特定行
- 2. 獲取特定列
- 3. 獲取特定的行和列
- 4. 獲取行切片
- 5. 獲取列切片
- 6. 獲取特定的行和列切片
- 示例代碼
df.iloc
和
df.loc
是 Pandas 中用于選擇數(shù)據(jù)的兩個(gè)重要函數(shù)。
df.iloc
基于整數(shù)位置索引,而
df.loc
基于標(biāo)簽(標(biāo)簽索引)選擇數(shù)據(jù)。以下是它們的常見用法匯總:
df.iloc
常見用法
df.iloc
是基于整數(shù)位置的選擇方法,用于按位置索引選擇數(shù)據(jù)。
1. 獲取特定行
-
獲取第一行:
first_row = df.iloc[0]
獲取 DataFrame 的第一行。
-
獲取最后一行:
last_row = df.iloc[-1]
獲取 DataFrame 的最后一行。
2. 獲取特定列
-
獲取第一列:
first_column = df.iloc[:, 0]
獲取 DataFrame 的第一列。
-
獲取最后一列:
last_column = df.iloc[:, -1]
獲取 DataFrame 的最后一列。
3. 獲取特定的行和列
-
獲取第一行和第一列的值:
value = df.iloc[0, 0]
獲取 DataFrame 的第一行第一列的值。
-
獲取最后一行和最后一列的值:
value = df.iloc[-1, -1]
獲取 DataFrame 的最后一行最后一列的值。
4. 獲取行切片
-
獲取前五行:
first_five_rows = df.iloc[:5]
獲取 DataFrame 的前五行。
-
獲取最后三行:
last_three_rows = df.iloc[-3:]
獲取 DataFrame 的最后三行。
5. 獲取列切片
-
獲取前兩列:
first_two_columns = df.iloc[:, :2]
獲取 DataFrame 的前兩列。
-
獲取從第二列到第四列:
middle_columns = df.iloc[:, 1:4]
獲取 DataFrame 的第二列到第四列(不包括第四列)。
6. 獲取特定的行和列切片
-
獲取前兩行和前兩列:
first_two_rows_and_columns = df.iloc[:2, :2]
獲取 DataFrame 的前兩行和前兩列。
-
獲取最后兩行和最后兩列:
last_two_rows_and_columns = df.iloc[-2:, -2:]
獲取 DataFrame 的最后兩行和最后兩列。
df.loc
常見用法
df.loc
是基于標(biāo)簽(標(biāo)簽索引)的選擇方法,用于按標(biāo)簽選擇數(shù)據(jù)。
1. 獲取特定行
- 獲取特定標(biāo)簽行:
獲取 DataFrame 中標(biāo)簽為row = df.loc['row_label']
'row_label'
的行。
2. 獲取特定列
- 獲取特定標(biāo)簽列:
獲取 DataFrame 中標(biāo)簽為column = df.loc[:, 'column_label']
'column_label'
的列。
3. 獲取特定的行和列
- 獲取特定標(biāo)簽的行和列的值:
獲取 DataFrame 中標(biāo)簽為value = df.loc['row_label', 'column_label']
'row_label'
的行和'column_label'
的列的值。
4. 獲取行切片
- 獲取標(biāo)簽范圍內(nèi)的行:
獲取 DataFrame 中從rows = df.loc['start_label':'end_label']
'start_label'
到'end_label'
的行。
5. 獲取列切片
- 獲取標(biāo)簽范圍內(nèi)的列:
獲取 DataFrame 中從columns = df.loc[:, 'start_column':'end_column']
'start_column'
到'end_column'
的列。
6. 獲取特定的行和列切片
- 獲取特定標(biāo)簽范圍內(nèi)的行和列:
獲取 DataFrame 中從rows_and_columns = df.loc['start_label':'end_label', 'start_column':'end_column']
'start_label'
到'end_label'
的行和從'start_column'
到'end_column'
的列。
示例代碼
import pandas as pd# 創(chuàng)建一個(gè)示例 DataFrame
data = {'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50], 'C': [100, 200, 300, 400, 500]}
df = pd.DataFrame(data, index=['a', 'b', 'c', 'd', 'e'])# iloc 示例
first_row = df.iloc[0]
last_row = df.iloc[-1]
first_column = df.iloc[:, 0]
last_column = df.iloc[:, -1]
value = df.iloc[0, 0]
first_five_rows = df.iloc[:5]
last_three_rows = df.iloc[-3:]
first_two_columns = df.iloc[:, :2]
middle_columns = df.iloc[:, 1:3]
first_two_rows_and_columns = df.iloc[:2, :2]
last_two_rows_and_columns = df.iloc[-2:, -2:]# loc 示例
row = df.loc['a']
column = df.loc[:, 'A']
value = df.loc['a', 'A']
rows = df.loc['a':'c']
columns = df.loc[:, 'A':'B']
rows_and_columns = df.loc['a':'c', 'A':'B']print("First row using iloc:")
print(first_row)
print("\nLast row using iloc:")
print(last_row)
print("\nFirst column using iloc:")
print(first_column)
print("\nLast column using iloc:")
print(last_column)
print("\nValue at first row and first column using iloc:")
print(value)
print("\nFirst five rows using iloc:")
print(first_five_rows)
print("\nLast three rows using iloc:")
print(last_three_rows)
print("\nFirst two columns using iloc:")
print(first_two_columns)
print("\nMiddle columns using iloc:")
print(middle_columns)
print("\nFirst two rows and columns using iloc:")
print(first_two_rows_and_columns)
print("\nLast two rows and columns using iloc:")
print(last_two_rows_and_columns)print("\nRow 'a' using loc:")
print(row)
print("\nColumn 'A' using loc:")
print(column)
print("\nValue at row 'a' and column 'A' using loc:")
print(value)
print("\nRows 'a' to 'c' using loc:")
print(rows)
print("\nColumns 'A' to 'B' using loc:")
print(columns)
print("\nRows 'a' to 'c' and columns 'A' to 'B' using loc:")
print(rows_and_columns)
這個(gè)代碼示例展示了如何使用 iloc
和 loc
進(jìn)行各種常見的數(shù)據(jù)選擇操作。