pandas.DataFrame.select_dtypes#

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

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

Parameters:
include, excludescalar 或 list-like

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

Returns:
DataFrame

包含 include 中的 dtype 并排除 exclude 中的 dtype 的框架子集。

Raises:
ValueError
  • 如果 includeexclude 都为空

  • 如果 includeexclude 具有重叠元素

  • 如果输入了任何类型的字符串 dtype。

参见

DataFrame.dtypes

返回包含每列数据类型的 Series。

Notes

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

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

  • 请参阅 numpy dtype hierarchy

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

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

  • 要选择 Pandas 分类 dtype,请使用 'category'

  • 要选择 Pandas 带时区的日期时间 dtype,请使用 'datetimetz''datetime64[ns, tz]'

Examples

>>> 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