博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pandas DataFrame索引和列属性
阅读量:2534 次
发布时间:2019-05-11

本文共 3223 字,大约阅读时间需要 10 分钟。

Pandas DataFrame index and columns attributes allow us to get the rows and columns label values.

Pandas DataFrame的索引和列属性允许我们获取行和列的标签值。

We can pass the integer-based value, , or arguments to get the label information.

我们可以传递基于整数的value, 或参数来获取标签信息。

熊猫DataFrame索引 (Pandas DataFrame index)

Let’s look into some examples of getting the labels of different rows in a DataFrame object. Before we look into the index attribute usage, we will create a sample DataFrame object.

让我们看一些获取DataFrame对象中不同行的标签的示例。 在研究索引属性的用法之前,我们将创建一个示例DataFrame对象。

import pandas as pdd1 = {'Name': ['John', 'Jane', 'Mary'], 'ID': [1, 2, 3], 'Role': ['CEO', 'CTO', 'CFO']}df = pd.DataFrame(d1, index=['A', 'B', 'C'])print('DataFrame:\n', df)

Output:

输出:

DataFrame:    Name  ID RoleA  John   1  CEOB  Jane   2  CTOC  Mary   3  CFO

1.获取单行的标签名称 (1. Getting Label Name of a Single Row)

row_1_label = df.index[1]print(type(row_1_label))  # 
print(row_1_label) # B

2.获取多行标签 (2. Getting Labels of Multiple Rows)

rows_labels = df.index[[1, 2]]print(type(rows_labels))  # 
print(rows_labels) # Index(['B', 'C'], dtype='object')

3.使用DataFrame索引切片 (3. Slicing with DataFrame index)

rows_labels = df.index[1:3]print(type(rows_labels))  # 
print(rows_labels) # Index(['B', 'C'], dtype='object')

4.具有DataFrame索引的布尔值 (4. Boolean with DataFrame index)

rows_labels = df.index[[True, False, True]]print(type(rows_labels))  # 
print(rows_labels) # Index(['A', 'C'], dtype='object')

We can’t set the rows label value using the DataFrame index attribute. If we try to do that, it will raise TypeError(“Index does not support mutable operations”).

我们无法使用DataFrame index属性设置行标签值。 如果我们尝试这样做,它将引发TypeError(“ Index不支持可变操作”)

df.index[0] = 'a'  # TypeError: Index does not support mutable operations

Python DataFrame列 (Python DataFrame columns)

The DataFrame columns attribute provides the label values for columns. It’s very similar to the index attribute. We can’t set the columns label value using this attribute. Let’s look at some examples of using the DataFrame columns attribute. We will reuse the earlier defined DataFrame object for these examples.

DataFrame列属性提供列的标签值。 它与index属性非常相似。 我们无法使用此属性设置列标签值。 让我们看一些使用DataFrame columns属性的示例。 对于这些示例,我们将重用先前定义的DataFrame对象。

1.获取单列标签 (1. Getting a Single Column Label)

column_1_label = df.columns[1]print(type(column_1_label))  # 
print(column_1_label) # ID

2.获取多列标签 (2. Getting Labels of Multiple Columns)

columns_labels = df.columns[[1, 2]]print(type(columns_labels))  # 
print(columns_labels) # Index(['ID', 'Role'], dtype='object')

3.使用DataFrame列进行切片 (3. Slicing with DataFrame columns)

columns_labels = df.columns[1:3]print(columns_labels)  # Index(['ID', 'Role'], dtype='object')

4.具有DataFrame列的布尔值 (4. Boolean with DataFrame columns)

columns_labels = df.columns[[False, False, True]]print(columns_labels)  # Index(['Role'], dtype='object')

结论 (Conclusion)

Pandas DataFrame index and columns attributes are helpful when we want to process only specific rows or columns. It’s also useful to get the label information and print it for future debugging purposes.

当我们只想处理特定的行或列时,Pandas DataFrame的索引和列属性很有用。 获取标签信息并打印以供将来调试时也很有用。

参考文献: (References:)

翻译自:

转载地址:http://wymzd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>