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

问题描述 投票:0回答:1

我试图根据数字和分类数据类型过滤列,然后为每个回归问题创建单独的列表。

问题是我无法使用.isin(['object','O'])执行此操作

列列表:

制造商157非null对象

模型157非空对象

Sales_in_thousands 157非null float64

four_year_resale_value 121非null float64

Vehicle_type 157非null对象

Price_in_thousands 155非null float64

Engine_size 156非null float64

马力156非零浮点64

轴距156非null float64

宽度156非null float64

Latest_Launch 157非null对象

Power_perf_factor 155非null float64

我想使用.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:

制造商对象

模型对象

Vehicle_type对象

Latest_Launch对象

python dataframe
1个回答
1
投票

有一个方便的助手功能,正是你想要做的,select_dtypes

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

df.select_dtypes(exclude=['O'])
© www.soinside.com 2019 - 2024. All rights reserved.