要按值类型提取数据帧列,可以使用select_dtypes
函数来选择指定类型的列。以下是一个代码示例:
import pandas as pd
# 创建数据帧
data = {'col1': [1, 2, 3, 4],
'col2': ['a', 'b', 'c', 'd'],
'col3': [1.1, 2.2, 3.3, 4.4]}
df = pd.DataFrame(data)
# 提取整数类型的列
int_cols = df.select_dtypes(include='int64')
print(int_cols)
# 提取字符串类型的列
str_cols = df.select_dtypes(include='object')
print(str_cols)
# 提取浮点类型的列
float_cols = df.select_dtypes(include='float64')
print(float_cols)
输出结果:
col1
0 1
1 2
2 3
3 4
col2
0 a
1 b
2 c
3 d
col3
0 1.1
1 2.2
2 3.3
3 4.4
在上述示例中,select_dtypes
函数的include
参数设置为想要提取的值类型,如'int64'
表示整数类型,'object'
表示字符串类型,'float64'
表示浮点类型。
上一篇:按值类型对对象进行排序。
下一篇:按值排序