DataFrame.select_dtypes() - 返回数据帧指定类型的列
DataFrame的select_dtypes方法返回具有指定数据类型的列。
方法原型
DataFrame.select_dtypes(self[, include, exclude])
参数:
- include:要返回的列的数据类型,标量或列表
- exclude:要排除的列的数据类型,标量或列表
返回值:
select_dtypes()方法返回原数据帧的子集,由include中声明的 列组成,并且排除exclude中声明的列。
示例代码
下面的代码使用字典创建一个数据帧对象:
>>> 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
下面的代码使用select_dtypes()方法返回仅包含bool列的子数据帧:
df.select_dtypes(include='bool') b 0 True 1 False 2 True 3 False 4 True 5 False ```
下面的代码使用select_dtypes()方法返回仅包含float64类型的列的子数据帧:
>>> df.select_dtypes(include=['float64'])
c
0 1.0
1 2.0
2 1.0
3 2.0
4 1.0
5 2.0
下面的代码使用select_dtypes()方法返回排除int类型的列的子数据帧:
>>> df.select_dtypes(exclude=['int'])
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