首页 > 解决方案 > 是否有任何代码可以将不同的数据类型隔离到不同的数组?

问题描述

horse.dtypes

surgery                   object
age                       object
hospital_number            int64
rectal_temp              float64
pulse                    float64
respiratory_rate         float64
temp_of_extremities       object
peripheral_pulse          object
mucous_membrane           object
capillary_refill_time     object
pain                      object
peristalsis               object
abdominal_distention      object
nasogastric_tube          object
nasogastric_reflux        object
nasogastric_reflux_ph    float64
rectal_exam_feces         object
abdomen                   object
packed_cell_volume       float64
total_protein            float64
abdomo_appearance         object
abdomo_protein           float64
surgical_lesion           object
lesion_1                   int64
lesion_2                   int64
lesion_3                   int64
cp_data                   object
dtype: object

我想通过创建一个新变量来隔离上述数据类型

例如:

object_vars = ['surgery', 'age','temp_of_extremities', 'peripheral_pulse',
   'mucous_membrane', 'capillary_refill_time', 'pain', 'peristalsis',
   'abdominal_distention', 'nasogastric_tube', 'nasogastric_reflux', 'rectal_exam_feces', 'abdomen','abdomo_appearance','surgical_lesion','cp_data'] 

是否有任何代码可以平滑过程并将它们分类为不同的变量

标签: pythonpandasnumpymachine-learningtypes

解决方案


我不确定我是否正确理解了您的问题,但以下代码将给出您作为示例给出的结果,即相同数据类型的所有列名的列表:

import pandas as pd

# create bogus data
o1 = object()
o2 = object()
i1 = 2
f1 = 5.

horse = pd.DataFrame([[o1,i1,f1,o2]],
                     columns = ("surgery", "lesion_1","pulse","pain"))

# list all present data_types
dtypes = horse.dtypes.unique()

# build dict with datatype name as key
data_by_type = {str(dtype): horse.loc[:,horse.dtypes==dtype].columns.to_list() for dtype in dtypes}
print(data_by_type["object"])
['surgery', 'pain']

推荐阅读