基于值 (Pandas) 过滤列:TypeError: 无法将 ['a'] 与块值进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40515418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:24:22  来源:igfitidea点击:

Filter columns based on a value (Pandas): TypeError: Could not compare ['a'] with block values

python-3.xpandas

提问by ramesh

I'm trying filter a DataFrame columns based on a value.

我正在尝试根据值过滤 DataFrame 列。

In[41]: df = pd.DataFrame({'A':['a',2,3,4,5], 'B':[6,7,8,9,10]})
In[42]: df
Out[42]: 
   A   B
0  a   6
1  2   7
2  3   8
3  4   9
4  5  10

Filtering columns:

过滤列:

In[43]: df.loc[:, (df != 6).iloc[0]]
Out[43]: 
   A
0  a
1  2
2  3
3  4
4  5

It works! But, When I used strings,

有用!但是,当我使用字符串时,

In[44]: df.loc[:, (df != 'a').iloc[0]]

I'm getting this error: TypeError: Could not compare ['a'] with block values

我收到此错误: TypeError: Could not compare ['a'] with block values

回答by SivaTP

You are trying to compare string 'a' with numeric values in column B.

您正在尝试将字符串 'a' 与 B 列中的数值进行比较。

If you want your code to work, first promote dtype of column B as numpy.object, It will work.

如果你想让你的代码工作,首先将 B 列的 dtype 提升为 numpy.object,它会工作。

df.B = df.B.astype(np.object)

Always check data types of the columns before performing the operations using

在执行操作之前总是检查列的数据类型使用

df.info()

回答by AlexG

You could do this with masks instead, for example:

你可以用面具来代替,例如:

df[df.A!='a'].A

and to filter from any column:

并从任何列中过滤:

df[df.apply(lambda x: sum([x_=='a' for x_ in x])==0, axis=1)]

回答by M.R.VaaN

The problem is due to the fact that there are numeric and string objects in the dataframe. You can loop through each column and check each column as a series for a specific value using

问题是由于数据框中存在数字和字符串对象。您可以遍历每一列并使用以下命令将每一列检查为特定值的系列

(Series=='a').any()

(系列=='a').any()