开发者

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

开发者 https://www.devze.com 2023-02-24 09:28 出处:网络 作者: 饺子大人
目录select_dtypes()的基本用法指定要提取的类型:参数include指定要排除的类型:参数excludepandas.DataFrame为每一列保存一个数据类型dtype。
目录
  • select_dtypes()的基本用法
    • 指定要提取的类型:参数include
    • 指定要排除的类型:参数exclude

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[LOrPT'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      floa开发者_Go教程t64
#android 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

printandroid(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提取特定类型列内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号