问题描述
我有一个看起来像这样的DataFrame:
I have a DataFrame looking like this:
ID Instrument Units Price Status
165 WTICO_USD -1 60.264 OPEN
169 WTICO_USD -1 60.274 OPEN
173 WTICO_USD 1 54.284 OPEN
185 BCO_USD 1 60.124 OPEN
如果我写以下内容,我会得到预期的结果:
If I write the following, I get what I expect:
DF[(DF.Instrument=='WTICO_USD')]
与此相同:
DF[(DF.Instrument=='WTICO_USD')&(DF.ID==165)]
但是,如果我尝试进一步过滤,像这样,我将没有行:
However, if I try to filter further, as in this, I get no rows:
DF[(DF.Instrument=='WTICO_USD')&(DF['Units']==-1)]
与
DF[(DF.Instrument=='WTICO_USD')&(DF.Units=='-1')]
但是,错误:
DF[(DF.Instrument=='WTICO_USD')&(DF['Units']>-1)]
因此,我尝试此操作并得到另一个错误:
So, I try this and get another error:
DF.Units.applymap(int)
因此,由于我在单位列中选择了所有值,所以我得到了一系列我无法兑换。但是,当我尝试使用此功能时,为什么也得到了呢?
So, since I select all values in the unit column, I get a series that I cannot convert. But why do I also get so when I try this?
DF[(DF.Instrument=='WTICO_USD')&(int(DF['Units'])>-1)]
如何过滤所有 Instrument的行== WTICO_USD
并具有单位< 0
?
推荐答案
您可以使用 DF.Units = DF.Units .map(int)
或 DF.Units = DF.Units.astype(int)
。从那里开始,您的过滤器将起作用。
You could use DF.Units = DF.Units.map(int)
or DF.Units = DF.Units.astype(int)
. From there your filters should work.
仅供参考中的map,applymap和apply方法之间的差异,很好地解释了 apply
和<$ c $之间的区别c>应用地图和地图
。您可能会注意到,根据方法 applymap
的定义,对于一系列而言,这是毫无意义的。
Just for reference Difference between map, applymap and apply methods in Pandas, gives a great explanation of the differences between apply
, applymap
, and map
. You may notice that under the definitions of the methods applymap
would be nonsensical on a series.
这篇关于Python Pandas过滤; TypeError:无法将系列转换为< class'int'>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!