Pandas DataFrame select_dtypes() 方法

定义和用法

select_dtypes() 方法返回新的 DataFrame,其中包含/排除指定 dtype 的列。

使用 include 参数来指定要包含的列,或使用 exclude 参数来指定要排除的列。

注意:您必须至少指定 include 和/或 exclude 参数之一,否则将出现错误。

实例

返回只包含 dtype 为 'int64' 的列的新 DataFrame:

import pandas as pd

df = pd.read_csv('data.csv')

newdf = df.select_dtypes(include='int64')

亲自试一试

语法

dataframe.select_dtypes(include, exclude)

参数

参数是关键字参数

参数 描述
include
  • 字符串
  • 列表
在结果中要包含的列。如果未指定 exclude 参数,则为必需项。
exclude
  • 字符串
  • 列表
在结果中要排除的列。如果未指定 include 参数,则为必需项。

返回值

包含所需列且已排除不需要列的 Pandas DataFrame