文章目录
df.iloc
和
df.loc
是 Pandas 中用于选择数据的两个重要函数。
df.iloc
基于整数位置索引,而
df.loc
基于标签(标签索引)选择数据。以下是它们的常见用法汇总:
df.iloc
常见用法
df.iloc
是基于整数位置的选择方法,用于按位置索引选择数据。
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
是基于标签(标签索引)的选择方法,用于按标签选择数据。
1. 获取特定行
- 获取特定标签行:
获取 DataFrame 中标签为row = df.loc['row_label']
'row_label'
的行。
2. 获取特定列
- 获取特定标签列:
获取 DataFrame 中标签为column = df.loc[:, 'column_label']
'column_label'
的列。
3. 获取特定的行和列
- 获取特定标签的行和列的值:
获取 DataFrame 中标签为value = df.loc['row_label', 'column_label']
'row_label'
的行和'column_label'
的列的值。
4. 获取行切片
- 获取标签范围内的行:
获取 DataFrame 中从rows = df.loc['start_label':'end_label']
'start_label'
到'end_label'
的行。
5. 获取列切片
- 获取标签范围内的列:
获取 DataFrame 中从columns = df.loc[:, 'start_column':'end_column']
'start_column'
到'end_column'
的列。
6. 获取特定的行和列切片
- 获取特定标签范围内的行和列:
获取 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
# 创建一个示例 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)
这个代码示例展示了如何使用 iloc
和 loc
进行各种常见的数据选择操作。