广告
返回顶部
首页 > 资讯 > 后端开发 > Python >pandas.DataFrame中提取特定类型dtype的列
  • 597
分享到

pandas.DataFrame中提取特定类型dtype的列

pandas DataFrame提取特定类型列pandas取dataframe特定列 2023-02-23 11:02:59 597人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录select_dtypes()的基本用法指定要提取的类型:参数include指定要排除的类型:参数excludepandas.DataFrame为每一列保存一个数据类型dtype

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'>})

到此这篇关于pandas.DataFrame中提取特定类型dtype的列的文章就介绍到这了,更多相关pandas DataFrame提取特定类型列内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: pandas.DataFrame中提取特定类型dtype的列

本文链接: https://www.lsjlt.com/news/197263.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作