简体   繁体   English

有没有办法在条件中使用多种数据类型过滤 Python 中的列?

[英]Is there a way to filter columns in Python using multiple data types in condition?

I am trying to filter columns based on numeric and categorical data types and then create separate list for each for Regression Problems.我正在尝试根据数字和分类数据类型过滤列,然后为每个回归问题创建单独的列表。

Problem is i am not able to do this using .isin(['object','O'])问题是我无法使用 .isin(['object','O'])

List of Columns:列列表:

Manufacturer 157 non-null object制造商 157 非空对象

Model 157 non-null object模型 157 非空对象

Sales_in_thousands 157 non-null float64 Sales_in_thousands 157 非空 float64

four_year_resale_value 121 non-null float64 Four_year_resale_value 121 非空 float64

Vehicle_type 157 non-null object Vehicle_type 157 非空对象

Price_in_thousands 155 non-null float64 Price_in_thousands 155 非空 float64

Engine_size 156 non-null float64 Engine_size 156 非空 float64

Horsepower 156 non-null float64马力 156 非空 float64

Wheelbase 156 non-null float64轴距 156 非空 float64

Width 156 non-null float64宽度 156 非空 float64

Latest_Launch 157 non-null object latest_Launch 157 非空对象

Power_perf_factor 155 non-null float64 Power_perf_factor 155 非空 float64

I want to do it using .isin([]) as multiple options can be passed in the list but its not working我想使用 .isin([]) 来做它,因为可以在列表中传递多个选项,但它不起作用

Below code doesn't work and I am looking for solutions for this code下面的代码不起作用,我正在寻找此代码的解决方案

df.dtypes.loc[df.dtypes.isin(['object','O'])]

Below code works but I dont like this way of writing code as if there are too many options then this code can get unnecessarily long & messy下面的代码有效,但我不喜欢这种编写代码的方式,好像有太多的选项,那么这段代码可能会变得不必要的冗长和混乱

df.dtypes.loc[(df.dtypes == ('object')) | (df.dtypes == ('O'))] 

Output:输出:

Manufacturer object制造商对象

Model object模型对象

Vehicle_type object Vehicle_type 对象

Latest_Launch object最新_启动对象

There's a handy helper function for exactly what you're trying to do, select_dtypes select_dtypes有一个方便的辅助函数,可以准确地执行您要执行的操作

df.select_dtypes(include=['O'])

df.select_dtypes(exclude=['O'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python数据框将过滤器应用于条件相同的多列? - Python data frame apply filter on multiple columns with same condition? 如何根据 python 中的条件更改多个 dataframe 列的数据类型? - How can I change the data types of multiple dataframe columns based on a condition in python? 使用多列条件过滤,Python 3.6 - Filter using multiple column condition, Python 3.6 如何使用 pandas/python 根据条件过滤列? - How to filter columns on the basis of a condition using pandas/python? 有没有一种简单的方法可以通过多列的内容过滤 Python 中的数据框? - Is there a simple way to filter a dataframe in Python by the contents of multiple columns? 在python / pandas中一次更改多列的数据类型 - changing data types of multiple columns at once in python/pandas 如何使用 python 按数据类型序列重新排列数据框列 - How to rearrange data frame columns by data types sequence using python TensorFlow使用生成器为具有不同数据类型的多个列创建数据集 - TensorFlow create a dataset using generator for multiple columns with different data types 根据多行条件过滤 pandas 列 - Filter pandas columns based on multiple row condition 使用 Pandas (Python) 基于单个条件将值分配给多个列 - Assigning values to multiple columns based on a single condition using Pandas (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM