Pandas使用 `iloc` 和 `loc` 常见用法汇总


df.ilocdf.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. 获取特定行
  • 获取特定标签行
    row = df.loc['row_label']
    
    获取 DataFrame 中标签为 'row_label' 的行。
2. 获取特定列
  • 获取特定标签列
    column = df.loc[:, 'column_label']
    
    获取 DataFrame 中标签为 'column_label' 的列。
3. 获取特定的行和列
  • 获取特定标签的行和列的值
    value = df.loc['row_label', 'column_label']
    
    获取 DataFrame 中标签为 'row_label' 的行和 'column_label' 的列的值。
4. 获取行切片
  • 获取标签范围内的行
    rows = df.loc['start_label':'end_label']
    
    获取 DataFrame 中从 'start_label''end_label' 的行。
5. 获取列切片
  • 获取标签范围内的列
    columns = df.loc[:, 'start_column':'end_column']
    
    获取 DataFrame 中从 'start_column''end_column' 的列。
6. 获取特定的行和列切片
  • 获取特定标签范围内的行和列
    rows_and_columns = df.loc['start_label':'end_label', 'start_column':'end_column']
    
    获取 DataFrame 中从 '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)

这个代码示例展示了如何使用 ilocloc 进行各种常见的数据选择操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今晚务必早点睡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值