首页 > pandas Series bool与运算

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的序列,有什么简单办法?


and 换为 &

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

【热门文章】
【热门文章】