pandas.DataFrame中提取特定类型dtype的列
作者:饺子大人 发布时间:2021-06-13 06:04:25
pandas.DataFrame为每一列保存一个数据类型dtype。
要仅提取(选择)特定数据类型为dtype的列,请使用pandas.DataFrame的select_dtypes()方法。
以带有各种数据类型的列的pandas.DataFrame为例。
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 1, 3],
'b': [0.4, 1.1, 0.1, 0.8],
'c': ['X', 'Y', 'X', 'Z'],
'd': [[0, 0], [0, 1], [1, 0], [1, 1]],
'e': [True, True, False, True]})
df['f'] = pd.to_datetime(['2018-01-01', '2018-03-15', '2018-02-20', '2018-03-15'])
print(df)
# a b c d e f
# 0 1 0.4 X [0, 0] True 2018-01-01
# 1 2 1.1 Y [0, 1] True 2018-03-15
# 2 1 0.1 X [1, 0] False 2018-02-20
# 3 3 0.8 Z [1, 1] True 2018-03-15
print(df.dtypes)
# a int64
# b float64
# c object
# d object
# e bool
# f datetime64[ns]
# dtype: object
将描述以下内容。
select_dtypes()的基本用法
指定要提取的类型:参数include
指定要排除的类型:参数exclude
select_dtypes()的基本用法
指定要提取的类型:参数include
在参数include中指定要提取的数据类型dtype。
print(df.select_dtypes(include=int))
# a
# 0 1
# 1 2
# 2 1
# 3 3
可以按原样指定作为Python的内置类型提供的那些变量,例如int和float。您可以将“ int”指定为字符串,也可以指定“ int64”(包括确切位数)。 (标准位数取决于环境)
print(df.select_dtypes(include='int'))
# a
# 0 1
# 1 2
# 2 1
# 3 3
print(df.select_dtypes(include='int64'))
# a
# 0 1
# 1 2
# 2 1
# 3 3
当然,当最多包括位数时,除非位数匹配,否则不会选择它。
print(df.select_dtypes(include='int32'))
# Empty DataFrame
# Columns: []
# Index: [0, 1, 2, 3]
列表中可以指定多种数据类型dtype。日期和时间datetime64 [ns]可以由’datetime’指定。
print(df.select_dtypes(include=[int, float, 'datetime']))
# a b f
# 0 1 0.4 2018-01-01
# 1 2 1.1 2018-03-15
# 2 1 0.1 2018-02-20
# 3 3 0.8 2018-03-15
可以将数字类型(例如int和float)与特殊值“ number”一起指定。
print(df.select_dtypes(include='number'))
# a b
# 0 1 0.4
# 1 2 1.1
# 2 1 0.1
# 3 3 0.8
元素为字符串str类型的列的数据类型dtype是object,但是object列还包含除str外的Python标准内置类型。实际上,数量并不多,但是,如示例中所示,如果有一列的元素为列表类型,请注意,该列也是由include = object提取的。
print(df.select_dtypes(include=object))
# c d
# 0 X [0, 0]
# 1 Y [0, 1]
# 2 X [1, 0]
# 3 Z [1, 1]
print(type(df.at[0, 'c']))
# <class 'str'>
print(type(df.at[0, 'd']))
# <class 'list'>
但是,除非对其进行有意处理,否则字符串str类型以外的对象都不会(可能)成为pandas.DataFrame的元素,因此不必担心太多。
指定要排除的类型:参数exclude
在参数exclude中指定要排除的数据类型dtype。您还可以在列表中指定多个数据类型dtype。
print(df.select_dtypes(exclude='number'))
# c d e f
# 0 X [0, 0] True 2018-01-01
# 1 Y [0, 1] True 2018-03-15
# 2 X [1, 0] False 2018-02-20
# 3 Z [1, 1] True 2018-03-15
print(df.select_dtypes(exclude=[bool, 'datetime']))
# a b c d
# 0 1 0.4 X [0, 0]
# 1 2 1.1 Y [0, 1]
# 2 1 0.1 X [1, 0]
# 3 3 0.8 Z [1, 1]
可以同时指定包含和排除,但是如果指定相同的类型,则会发生错误。
print(df.select_dtypes(include='number', exclude=int))
# b
# 0 0.4
# 1 1.1
# 2 0.1
# 3 0.8
# print(df.select_dtypes(include=[int, bool], exclude=int))
# ValueError: include and exclude overlap on frozenset({<class 'numpy.int64'>})
来源:https://blog.csdn.net/qq_18351157/article/details/109745683
猜你喜欢
- 无论安装何版本的mysql,在管理工具的服务中启动mysql服务时都会在中途报错。内容为:在 本地计算机 无法启动mysql服务 错误106
- 动态加载JavaScript文件和CSS资源为Web前端开发提供了巨大的灵活性,同时也实现了lazy load和按需加载,相比XMLHttp
- 测试驱动开发(TDD)是一个迭代的开发周期,强调编写实际代码之前编写自动化测试。这个过程很简单: 先编写
- 如何在线更改密码?<%id = Request("id")newpassword =
- 1. 简介本文将介绍 Go 语言中的 sync.Cond 并发原语,包括 sync.Cond的基本使用方法、实现原理、使用注意事项以及常见的
- 项目地址https://github.com/jonssonyan...开发工具 python 3.7.9pycharm 2019.3.5
- JavaScript: <script type="text/javascript"> var level1
- Python中的布尔类型Python中的布尔类型(bool)只有两个取值,分别是True和False。bool类型通常用于逻辑判断和条件控制
- 1、执行环境说明python版本3.7直接使用pip进行安装pywin32、pyinstallerpip install pywin32pi
- Pytorch的核心是两个主要特征:1.一个n维tensor,类似于numpy,但是tensor可以在GPU上运行2.搭建和训练神经网络时的
- 这篇文章主要介绍了python next()和iter()函数原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 要选择有助于使查询执行更快的列,应遵循如下规则(这里,“BLOB 类型”应该理解为即包含B L O
- 我们的规范到底做到哪一步算是发挥良好的价值?其实一件事物我们理解错根本目的会导致出大不一样的结果,直接反应在设计师到底要体现什么的价值。想想
- detectres.asp<HTML><head><TITLE>asp教程之全能屏幕分辨率侦测</
- 服务端:#!/usr/bin/envimport SocketServerclass myMonitorHandler(SocketServ
- 【基本介绍】【格式】:pivot(聚合函数 for 需要转为列的字段名 in(需要转为列的字段值))【说明】:实现将指定字段的字段值转换为列
- Python处理json字符串中的非法双引号工作中数据清洗时遇到以下情况:a = '{"地区": "湖
- 例子:def re_escape(fn): def arg_escaped(this, *args):&
- 适配器模式说明说明: 适配器模式,一般是为要使用的接口,不符本应用或本系统使用,而需引入的中间适配层类或对象的情况;场景: 就好比我们买了台
- 数据集数据集为Barcelona某段时间内的气象数据,其中包括温度、湿度以及风速等。本文将简单搭建来对风速进行预测。特征构造对于风速的预测,