Lou_Ellis_1998
Lou_Ellis_1998

Reputation: 3

How to apply If Else statements in Pandas Dataframe?

I have a Pandas Dataframe and I am trying to add a new column where the value in the new column is dependant on some conditions in the existing Dataframe. The Dataframe I have is as follows:

Date / Time Open High Low Close Volume
2020-06-02 16:30:00 1.25566 1.25696 1.25439 1.25634 2720
2020-06-02 17:00:00 1.25638 1.25683 1.25532 1.25614 2800
2020-06-02 17:30:00 1.25615 1.25699 1.25520 1.25565 2827
2020-06-02 18:00:00 1.25565 1.25598 1.25334 1.25341 2993
2020-06-02 18:30:00 1.25341 1.25385 1.25272 1.25287 1899
2020-07-03 07:00:00 1.24651 1.24673 1.24596 1.24603 600
2020-07-03 07:30:00 1.24601 1.24641 1.24568 1.24594 487
2020-07-03 08:00:00 1.24593 1.24618 1.24580 1.24612 455
2020-07-03 08:30:00 1.24612 1.24667 1.24603 1.24666 552
2020-07-03 09:00:00 1.24666 1.24785 1.24623 1.24765 922

I would like to add a new column called 'Signal', which is dependant on the following if and else statements. I have written these into a Function:

def BullEngulf(df):   
    if (df['Open'] <= df['Close'].shift(1)) and (df['Close'] > df['Open'].shift(1)) and (df['Close'].shift(1) < df['Open'].shift(1)):
    return 'Open'
    else:
    return '0'

I have then tried to apply the function as follows:

df['Signal'] = df.apply(BullEngulf)
df

When I run the code, the following error message occurs:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-62-f87af322a959> in <module>
      5         return '0'
      6 
----> 7 df['Signal'] = df.apply(BullEngulf)
      8 df

~\anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, raw, result_type, args, **kwds)
   6876             kwds=kwds,
   6877         )
-> 6878         return op.get_result()
   6879 
   6880     def applymap(self, func) -> "DataFrame":

~\anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self)
    184             return self.apply_raw()
    185 
--> 186         return self.apply_standard()
    187 
    188     def apply_empty_result(self):

~\anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
    293 
    294             try:
--> 295                 result = libreduction.compute_reduction(
    296                     values, self.f, axis=self.axis, dummy=dummy, labels=labels
    297                 )

pandas\_libs\reduction.pyx in pandas._libs.reduction.compute_reduction()

pandas\_libs\reduction.pyx in pandas._libs.reduction.Reducer.get_result()

<ipython-input-62-f87af322a959> in BullEngulf(df)
      1 def BullEngulf(df):
----> 2     if (df['Open'] <= df['Close'].shift(1)) and (df['Close'] > df['Open'].shift(1)) and (df['Close'].shift(1) < df['Open'].shift(1)):
      3         return 'Open'
      4     else:
      5         return '0'

~\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   4403         k = self._convert_scalar_indexer(k, kind="getitem")
   4404         try:
-> 4405             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4406         except KeyError as e1:
   4407             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: 'Open'

Please could somebody explain what is wrong with the code above?

Thanks

Upvotes: 0

Views: 224

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150825

Try:

df['Signal'] = np.where((df['Open'] <= df['Close'].shift(1)) &
                        (df['Close'] > df['Open'].shift(1))  &
                        (df['Close'].shift(1) < df['Open'].shift(1)),
                        'Open', '0')

Upvotes: 1

Related Questions