pandas.Series.where与字典不兼容

时间:2019-12-20 22:44:04

标签: python pandas

我想用链式操作过滤熊猫系列:

s = pd.Series([1,2])
# instead of using s twice:
s[s > 1]
# I want to use method to chain the operations
s.where(lambda x:x > 1).dropna()

当Sereis中的值是纯数字时,链接方法效果很好:

s.where(lambda x:x > 1).dropna()

1    2.0
dtype: float64

但是,当系列中的值是字典时,它不起作用:

s = pd.Series([{"v":1},{"v":2}])
s.where(lambda x:x["v"] > 1).dropna()

bellow是错误消息:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-494-1db694eb5c5d> in <module>()
      1 s = pd.Series([{"v":1},{"v":2}])
----> 2 s.where(lambda x:x["v"] > 1).dropna()

~/anaconda3/envs/python3/lib/python3.6/site-packages/pandas/core/generic.py in where(self, cond, other, inplace, axis, level, errors, try_cast, raise_on_error)
   8832         other = com.apply_if_callable(other, self)
   8833         return self._where(cond, other, inplace, axis, level,
-> 8834                            errors=errors, try_cast=try_cast)
   8835 
   8836     @Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False",

~/anaconda3/envs/python3/lib/python3.6/site-packages/pandas/core/generic.py in _where(self, cond, other, inplace, axis, level, errors, try_cast)
   8563 
   8564         # align the cond to same shape as myself
-> 8565         cond = com.apply_if_callable(cond, self)
   8566         if isinstance(cond, NDFrame):
   8567             cond, _ = cond.align(self, join='right', broadcast_axis=1)

~/anaconda3/envs/python3/lib/python3.6/site-packages/pandas/core/common.py in apply_if_callable(maybe_callable, obj, **kwargs)
    327 
    328     if callable(maybe_callable):
--> 329         return maybe_callable(obj, **kwargs)
    330 
    331     return maybe_callable

<ipython-input-494-1db694eb5c5d> in <lambda>(x)
      1 s = pd.Series([{"v":1},{"v":2}])
----> 2 s.where(lambda x:x["v"] > 1).dropna()

~/anaconda3/envs/python3/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    866         key = com.apply_if_callable(key, self)
    867         try:
--> 868             result = self.index.get_value(self, key)
    869 
    870             if not is_scalar(result):

~/anaconda3/envs/python3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4373         try:
   4374             return self._engine.get_value(s, k,
-> 4375                                           tz=getattr(series.dtype, 'tz', None))
   4376         except KeyError as e1:
   4377             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: 'v'

我的问题是如何使用where方法正确过滤熊猫系列,感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

x的可调用项中的

where是系列。因此,您需要执行以下操作

s.where(lambda x: x.str['v']>1 ).dropna()

Out[47]:
1    {'v': 2}
dtype: object