pandas.DataFrame.select_dtypes#

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

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

参数
包括、排除标量或类似列表

要包括或排除的数据类型或字符串的选择。必须至少提供其中一个参数。

退货
DataFrame

帧的子集,包括中的数据类型 include 并排除中的数据类型 exclude

加薪
ValueError
  • 如果两者都是 includeexclude 都是空的

  • 如果 includeexclude 具有重叠的元素

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

参见

DataFrame.dtypes

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

注意事项

  • 要全选,请执行以下操作 数字 类型,使用 np.number'number'

  • 若要选择字符串,必须使用 object Dtype,但请注意,这将返回 all 对象数据类型列

  • 请参阅 numpy dtype hierarchy

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

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

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

  • 要选择Pandas Date timetz dtype,请使用 'datetimetz' (0.20.0中的新功能)或 '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