获取pandas DataFrame中所有数值列的名称(按dtype过滤)[duplicate]

14ifxucb  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(119)

此问题在此处已有答案

How do I find numeric columns in Pandas?(15个回答)
16天前关闭
哪些pandas方法可以用一行代码获得给定DataFrame中具有数字dtypes(所有大小,如uint8,而不仅仅是64位的)的列的名称?注意:实际上有数百列,因此dtypes(或其他向量化方法)应该用于检测数据类型。

import numpy as np
import pandas as pd

test_df = pd.DataFrame(data=[{"str_col": "some string",
                              "int_col": 0,
                              "float_col": 3.1415}])

test_df.dtypes[test_df.dtypes == np.dtype('float')].index.values[0]
# "float_col"

test_df.dtypes[test_df.dtypes == np.dtype('int')].index.values[0]
# "int_col"

# ?
# ["float_col", "int_col"]

字符串

41zrol4v

41zrol4v1#

要获取数值列,请执行以下操作:

numeric_cols = test_df.select_dtypes(include=['number']).columns.tolist()

字符串
测试结果:

['int_col', 'float_col']

相关问题