pandas.DataFrame.select_dtypes#

DataFrame.select_dtypes(include=None, exclude=None)[源代码][源代码]#

根据列的数据类型返回DataFrame列的子集。

参数:
包含, 排除标量或类列表

要包含/排除的 dtypes 或字符串的选择。必须至少提供这些参数中的一个。

返回:
DataFrame

包含 include 中的dtypes并且排除 exclude 中的dtypes的帧子集。

引发:
ValueError
  • 如果 includeexclude 都为空

  • 如果 includeexclude 有重叠的元素

TypeError
  • 如果传递了任何类型的字符串数据类型。

参见

DataFrame.dtypes

返回每列数据类型的系列。

备注

  • 要选择所有 数字 类型,请使用 np.number'number'

  • 要选择字符串,必须使用 object dtype,但请注意,这将返回 所有 object dtype 列。启用 pd.options.future.infer_string 后,使用 "str" 将可以用来选择所有字符串列。

  • 请参见 numpy 数据类型层次结构

  • 要选择日期时间,请使用 np.datetime64'datetime''datetime64'

  • 要选择时间增量,请使用 np.timedelta64'timedelta''timedelta64'

  • 要选择 Pandas 的分类数据类型,请使用 'category'

  • 要选择 Pandas 的 datetimetz dtypes,请使用 'datetimetz''datetime64[ns, tz]'

例子

>>> df = pd.DataFrame(
...     {"a": [1, 2] * 3, "b": [True, False] * 3, "c": [1.0, 2.0] * 3}
... )
>>> df
        a      b  c
0       1   True  1.0
1       2  False  2.0
2       1   True  1.0
3       2  False  2.0
4       1   True  1.0
5       2  False  2.0
>>> df.select_dtypes(include="bool")
   b
0  True
1  False
2  True
3  False
4  True
5  False
>>> df.select_dtypes(include=["float64"])
   c
0  1.0
1  2.0
2  1.0
3  2.0
4  1.0
5  2.0
>>> df.select_dtypes(exclude=["int64"])
       b    c
0   True  1.0
1  False  2.0
2   True  1.0
3  False  2.0
4   True  1.0
5  False  2.0