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


猜你喜欢
- 看代码吧~// Strval 获取变量的字符串值// 浮点型 3.0将会转换成字符串3, "3"// 非数值或字符类型的
- 1.首先到jetbrains下载专业版 https://www.jetbrains.com/pycharm/download/#sectio
- 前言目前学习python几个月了,由于自己比较喜欢跑马拉松,已经跑过了很多场比赛,前些天就写了个简单的爬虫爬取了网上三千多场马拉松比赛的报名
- pyquery库是jQuery的Python实现,可以用于解析HTML网页内容,使用方法:from pyquery import PyQue
- 1、下载gliffy-confluence-plugin-9.1.2.obr2、解压后找到GliffyLicenseManager.clas
- 一 Insert 语句1.在数据中只插入默认值:insert into [DBTrain].[dbo].[log_info]default
- 前提1. Cookie 是脆弱的。cookie 容易被窃取和受到垮站脚本的攻击,我们必须接受 cookie 是不安全的。2. 持久化登录 c
- 前言图片的本质就是大量像素在二维平面上的组合,每个像素点用数字化方式记录颜色。可以直观的想象,一张图片就是一个巨大的电子栅格,每个格子内有一
- 我就废话不多说了,大家还是直接看代码吧~func main() { var a chan string a =mak
- 这段时间一直想尝试着在SAE中使用Python,初步选择了Web.py框架做为开发框架,但是可怜SAE上的资料少的可怜,有点问题基本上解决不
- python 实现单例的方法第一种方法:使用基类New 是真正创建实例对象的方法,所以重写基类的new 方法,以此保证创建对象的时候只生成一
- 一,前言上篇,主要介绍了在 Vue 的数据初始化流程中,对象属性的单层劫持是如何实现的回顾一下,主要涉及以下几个核心点:data 为函数和对
- 本文实例为大家分享了python地震数据可视化的具体代码,供大家参考,具体内容如下参考源码:seisplot准备工作:在windows10下
- Python 中有 while 和 for 两种循环机制,其中 while 循环
- 两行JavaScript代码的QQ窗口抖动效果<img id="win" style='pos
- anaconda 集成了很多科学计算中所需要的包,如numpy,scipy等等,具体查看anaconda中已经预先安装配置好的包
- banner 设计会严重影响广告投放效果,在此建议相关设计人员,在设计FLASH BANNER的时候考虑到以下问题.1.
- 本文实例讲述了Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)。分享给大家供大家参考,具体
- class EntryDemo( Frame ): """Demonstrate Entrys and Eve
- Java 实现class PrimeNumber{public static void main(String[] args) {long