abyrwalg
abyrwalg

Reputation: 31

Add new column in pandas dataframe with if-else conditions for existing column

I want to add new column ( 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.

Upvotes: 1

Views: 476

Answers (1)

jezrael
jezrael

Reputation: 862511

The best is working with Series, not loop. Universal solution is use numpy.where or mask for change values by condition:

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))

Upvotes: 2

Related Questions