pandas Series bool与运算

In[101]: x.head()
Out[101]: 

0    1.559343
1    1.103526
2    0.791218
3    0.946919
4    1.120411
Name: Beta, dtype: float64

In[102]: x[~np.isnan(x) and x > x.mean() + 3 * x.std()] = x.mean() + 3 * x.std()
Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-102-4a0654e0fc5c>", line 1, in <module>
    x[~np.isnan(x) and x > x.mean() + 3 * x.std()] = x.mean() + 3 * x.std()
  File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 892, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

In[103]: ~np.isnan(x)
Out[103]: 

0        True
1        True
2        True
3        True
4        True
5        True
6        True
7        True
8        True
9        True
10       True
11       True

Series不能对两个bool值进行and运算吗,bool值也是pandas.Series的序列,有什么简单办法?

阅读 11k
2 个回答

and 换为 &

x[~np.isnan(x) & x > x.mean() + 3 * x.std()]

新手上路,请多包涵

我也遇到了这个问题 能不能说下原理?跪谢

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题