python - 是否有任何代码可以将不同的数据类型隔离到不同的数组?
问题描述
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']
是否有任何代码可以平滑过程并将它们分类为不同的变量
解决方案
我不确定我是否正确理解了您的问题,但以下代码将给出您作为示例给出的结果,即相同数据类型的所有列名的列表:
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']
推荐阅读
- html - Centering text inside div in navbar
- go - 在自定义 Vault 插件上使用 vault.NewTestCluster 运行测试时取消引用 nil 指针
- encoding - 是否有可能与 base64 编码/解码发生冲突
- wordpress - Azure的应用服务有Bug
- sesam - 使用 DTL 在 Sesam 中展平深层实体结构
- typescript - 类型脚本 - 使用键 ID 合并数组中的对象
- vue.js - VueJs 组件名称保留字?
- javascript - JavaScript 中的 JSON 数组计数
- php - 根据 Woocommerce 结帐中的复选框自定义字段在项目名称下添加文本
- javascript - 函数@ lint 脚本失败