当前位置: 首页 > news >正文

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

文章目录

      • `df.iloc` 常见用法
        • 1. 获取特定行
        • 2. 获取特定列
        • 3. 获取特定的行和列
        • 4. 获取行切片
        • 5. 获取列切片
        • 6. 获取特定的行和列切片
      • `df.loc` 常见用法
        • 1. 获取特定行
        • 2. 获取特定列
        • 3. 获取特定的行和列
        • 4. 获取行切片
        • 5. 获取列切片
        • 6. 获取特定的行和列切片
      • 示例代码

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 进行各种常见的数据选择操作。

http://www.lryc.cn/news/401858.html

相关文章:

  • 【linux服务器】大语言模型实战教程:LLMS大模型快速部署到个人服务器
  • Windows 32 汇编笔记(二):使用 MASM
  • 手机和电脑通过TCP传输(一)
  • Sentinel规则持久化Push模式两种实现方式
  • Spring Boot 中使用 Resilience4j 实现弹性微服务的简单了解
  • Hadoop3:MR程序压测实验
  • 初学者如何通过建立个人博客盈利
  • 构建稳健性:如何在Gradle中配置构建失败时的行为
  • 大语言模型-基础及拓展应用
  • STM32使用Wifi连接阿里云
  • 2024.7.16日 最新版 docker cuda container tookit下载!
  • 打印室预约小程序的设计
  • Android音视频—OpenGL 与OpenGL ES简述,渲染视频到界面基本流程
  • Vscode中Github copilot插件无法使用(出现感叹号)解决方案
  • Spring-cloud-openfeign-@FeignClient中的configuration属性
  • 实验七:图像的复原处理
  • 前端面试题日常练-day94 【Less】
  • c 语言 中 是否有 unsigned 安;这种写法?
  • Hive第三天
  • 【C++】模版初阶以及STL的简介
  • 51单片机学习(4)
  • 3D问界—MAYA制作铁丝栅栏(透明贴图法)
  • 编译器对C++23的支持程度
  • k8s核心操作_存储抽象_K8S中使用Secret功能来存储密码_使用免密拉取镜像_k8s核心实战总结---分布式云原生部署架构搭建033
  • 21集 ESP32-IDF开发教程-《MCU嵌入式AI开发笔记》
  • 《大数据基础》相关知识点及考点,例题
  • 网络通信介绍
  • 16、Python之容器:元组与列表、推导式与生成式,差之毫厘谬以千里
  • HTTP协议——请求头和请求体详情
  • 编程中的智慧之设计模式二