[英]Add new column in pandas dataframe with if-else conditions for existing column
I want to add new column ( shop_yy['tt']
) 我想添加新列( shop_yy['tt']
)
for row in shop_yy['y']:
if row > shop_yy['3sigmaM'] or row < shop_yy['3sigmaL'] :
shop_yy['tt'] = pd.rolling_mean(shop_yy['y'],window=5)
else:
shop_yy['tt'] = shop_yy['y']
But I see this mistake: 但是我看到这个错误:
/usr/local/lib/python3.4/dist-packages/ipykernel_launcher.py:10: FutureWarning: pd.rolling_mean is deprecated for Series and will be removed in a future version, replace with
Series.rolling(center=False,window=3).mean()
# Remove the CWD from sys.path while we load stuff.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-24-2d3ac684a2df> in <module>()
13 shop_yy['3sigmaL']=shop_yy['MA'] - 3*shop_yy['std']
14 for row in shop_yy['y']:
---> 15 if row > shop_yy['3sigmaM'] or row < shop_yy['3sigmaL'] :
16 shop_yy['tt'] = pd.rolling_mean(shop_yy['y'],window=5)
17 else:
/usr/local/lib/python3.4/dist-packages/pandas-0.19.2-py3.4-linux-x86_64.egg/pandas/core/generic.py in __nonzero__(self)
915 raise ValueError("The truth value of a {0} is ambiguous. "
916 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 917 .format(self.__class__.__name__))
918
919 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Please, help. 请帮忙。
The best is working with Series
, not loop. 最好是使用Series
而不是循环。 Universal solution is use numpy.where
or mask
for change values by condition: 通用解决方案是使用numpy.where
或mask
根据条件更改值:
m = (shop_yy['y'] > shop_yy['3sigmaM']) | (shop_yy['y'] < shop_yy['3sigmaL'])
shop_yy['tt'] = np.where(m, pd.rolling_mean(shop_yy['y'],window=5), shop_yy['y'])
Or: 要么:
shop_yy['tt'] = shop_yy['tt'].mask(m, pd.rolling_mean(shop_yy['y'],window=5))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.