Pandas DataFrame select_dtypes() 方法
示例
返回一个仅包含 dtype 为 'int64' 的列的 DataFrame
import pandas as pd
df = pd.read_csv('data.csv')
newdf = df.select_dtypes(include='int64')
自己动手试一试 »
定义和用法
select_dtypes()
方法返回一个包含/排除指定 dtype 列的新 DataFrame。
使用 include
参数指定包含的列,或使用 exclude
参数指定要排除的列。
注意:您必须指定 include
和/或 exclude
参数中的至少一个,否则将引发错误。
语法
dataframe.select_dtypes(include, exclude)
参数
参数是 关键字参数。
参数 | 值 | 描述 |
---|---|---|
include | 字符串 | 列表 | 结果中要包含的列。如果 exclude 参数不存在,则需要此参数。 |
exclude | 字符串 | 列表 | 结果中要排除的列。如果 include 参数不存在,则需要此参数。 |
返回值
一个包含包含列且不包含排除列的 Pandas DataFrame。