本文介绍了获取具有对象或分类数据类型的列名列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是获取一个列表对象:['assetCode', 'assetName'],其中的内容是检索到的 Panda.series 的标签基于一个以上的条件.我试过了:

My goal is to get a list object: ['assetCode', 'assetName'], where the contents are the labels of a Panda.series that are retrieved based on more than one condition. I tried:

tmp3 = datatype[datatype == 'object' | datatype == 'category'].index # extract label from Pandas.series

这给出了错误:TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]

然而,虽然不太优雅,但我能够找到以下两个可行的解决方案:

However, while less elegant, I was able to find the following two working solutions:

tmp2 = datatype[datatype == 'object'].index # extract label from Pandas.series
tmp2[0]
'assetCode'


tmp1 = datatype[datatype == 'category'].index # extract label from Pandas.series
tmp1[0]
'assetName'

如何将这两个字符串组合成一个列表对象?有没有比我尝试的方式更好的方法来实现这个目标?

How do I combine these two strings into a list object? Is there a better way to achieve that goal than the way I am trying to do it?

推荐答案

设置

df

   A  B  C
0  8  4  2
1  8  8  6
2  8  5  2

datatype = df.dtypes
datatype

A      object
B    category
C       int64
dtype: object

您似乎正在尝试从某个 DataFrame(此处未显示)中选择对象和分类列.要修复您的代码,请使用:


It looks like you are trying to select object and categorical columns from some DataFrame (not shown here). To fix your code, use:

tmp3 = datatype[(datatype == 'object') | (datatype == 'category')].index.tolist()
tmp3
#  ['A', 'B']

由于按位运算符具有更高的优先级,您需要在对掩码进行 OR 运算之前使用括号.之后,索引工作正常.

Since bitwise operators have higher precedence, you will need to use parentheses before ORing the masks. After that, indexing works fine.

要获取列表,请调用 .index.tolist().

To get a list, call .index.tolist().

另一个解决方案是select_dtypes:

df.select_dtypes(include=['object', 'category'])

   A  B
0  8  4
1  8  8
2  8  5

df.select_dtypes(include=['object', 'category']).columns
# ['A', 'B']

这避免了对中间datatype系列的需求.

This circumvents the need for an intermediate datatype series.

这篇关于获取具有对象或分类数据类型的列名列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:46