Append value to df row using lambda if else

Question:

hello experts i was struck in appending value to new column of df by comparing other column values. i searched related questions but not found suitable answer. I’m newbie to python

I want to append new column of df at last row of each element by comparing last 4 values of df other column using df.apply lambda help in this regard highly appreciated.

I have following df:

    Symbol    open      close      sig
0   APPL      153.60    152.90      0
1   APPL      152.90    153.55      1
2   APPL      153.55    152.00      0
3   APPL      152.00    153.50      1
4   APPL      153.50    154.10      1

5   TSLA      193.00    192.10      0
6   TSLA      192.10    191.50      0
7   TSLA      191.50    192.90      1
8   TSLA      192.90    192.45      0
9   TSLA      192.45    191.10      0

i want to compare df[‘sig’] column except 1 row and so on for all stocks, if sig column last 4 values APPL is 1011 then df [‘signal’] at row 4 it should be appended as 1 (i.e. last row of each stock) if last 4 values df[‘sig’] of TSLA is 0100 then at 9 row df[‘signal’] to be appended as 0

by using lambda or df.npwhere etc…

Thanks!

expected this:

    Symbol    open      close      sig    signal
0   APPL      153.60    152.90      0      NaN
1   APPL      152.90    153.55      1      NaN
2   APPL      152.75    152.00      0      NaN
3   APPL      153.00    153.50      1      NaN
4   APPL      153.50    154.10      1      1

5   TSLA      193.00    192.10      0      NaN
6   TSLA      192.10    191.50      0      NaN
7   TSLA      191.50    192.90      1      NaN
8   TSLA      192.90    192.45      0      NaN
9   TSLA      192.45    191.10      0      0
Asked By: B Sailoo

||

Answers:

You can use a groupby.rolling:

df['signal'] = (df.groupby('Symbol')['sig'].rolling(4)
                  .apply(lambda x: x.eq([1, 0, 1, 1]).all()).droplevel(0)
               )

Output:

  Symbol    open   close  sig  signal
0   APPL  153.60  152.90    0     NaN
1   APPL  152.90  153.55    1     NaN
2   APPL  152.75  152.00    0     NaN
3   APPL  153.00  153.50    1     0.0
4   APPL  153.50  154.10    1     1.0
5   TSLA  193.00  192.10    0     NaN
6   TSLA  192.10  191.50    0     NaN
7   TSLA  191.50  192.90    1     NaN
8   TSLA  192.90  192.45    0     0.0
9   TSLA  192.45  191.10    0     0.0

mapping custom values for each group:

mapper = {'APPL': ([1, 0, 1, 1], 1), 'TSLA': ([0, 1, 0, 0], 0)}

df['signal'] = (
    df.groupby('Symbol')['sig']
      .apply(lambda g: g.rolling(4)
                        .apply(lambda x: mapper[g.name][1]
                               if x.tolist() == mapper[g.name][0]
                               else float('nan')
                              )
            )
)

Output:

  Symbol    open   close  sig  signal
0   APPL  153.60  152.90    0     NaN
1   APPL  152.90  153.55    1     NaN
2   APPL  153.55  152.00    0     NaN
3   APPL  152.00  153.50    1     NaN
4   APPL  153.50  154.10    1     1.0
5   TSLA  193.00  192.10    0     NaN
6   TSLA  192.10  191.50    0     NaN
7   TSLA  191.50  192.90    1     NaN
8   TSLA  192.90  192.45    0     NaN
9   TSLA  192.45  191.10    0     0.0
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.