Create new column based on condtions of others

Question:

I have this df:

   Segnale  Prezzo  Prezzo_exit
0   Long    44645   43302
1   Short   41169   44169
2   Long    44322   47093
3   Short   45323   42514

sample code to generate it:

tbl2 = {
      "Segnale" : ["Long", "Short", "Long", "Short"],
      "Prezzo" : [44645, 41169, 44322, 45323],
    "Prezzo_exit" : [43302, 44169, 47093, 42514]}

df = pd.DataFrame(tbl2)

I need to create a new column named "esito" with this conditions:

if df["Segnale"] =="Long" and df["Prezzo"] < df["Prezzo_exit"]  #row with "target"
if df["Segnale"] =="Long" and df["Prezzo"] > df["Prezzo_exit"]  #row with "stop"

if df["Segnale"] =="Short" and df["Prezzo"] < df["Prezzo_exit"]  #row with "stop"
if df["Segnale"] =="Short" and df["Prezzo"] > df["Prezzo_exit"]  #row with "target"

So the final result will be:

    Segnale Prezzo  Prezzo_exit esito
  0 Long    44645   43302       stop
  1 Short   41169   44169       stop
  2 Long    44322   47093       target
  3 Short   45323   42514       target

I tried with no success:

  df.loc[(df['Segnale'].str.contains('Long') & df['Prezzo'] < 
                             df['Prezzo_exit']), 'Esito'] = 'Target'
  df.loc[(df['Segnale'].str.contains('Long') & df['Prezzo'] > df['Prezzo_exit']), 'Esito'] = 
                                                                              'Stop'

  df.loc[(df['Segnale'].str.contains('Short') & df['Prezzo'] > df['Prezzo_exit']), 'Esito'] = 
                                                                              'Target'
  df.loc[(df['Segnale'].str.contains('Short') & df['Prezzo'] > df['Prezzo_exit']), 'Esito'] = 
                                                                         'Stop'
Asked By: Pren Ven

||

Answers:

This will do what your question asks:

df.loc[(df.Segnale=='Long') & (df.Prezzo < df.Prezzo_exit), 'esito'] = 'target'
df.loc[(df.Segnale=='Long') & (df.Prezzo > df.Prezzo_exit), 'esito'] = 'stop'
df.loc[(df.Segnale=='Short') & (df.Prezzo < df.Prezzo_exit), 'esito'] = 'stop'
df.loc[(df.Segnale=='Short') & (df.Prezzo > df.Prezzo_exit), 'esito'] = 'target'

Output:

  Segnale  Prezzo  Prezzo_exit   esito
0    Long   44645        43302    stop
1   Short   41169        44169    stop
2    Long   44322        47093  target
3   Short   45323        42514  target

UPDATE:

You could also do this:

df['esito'] = ( pd.Series(['stop']*len(df)).where(
    ((df.Segnale=='Long') & (df.Prezzo > df.Prezzo_exit)) | ((df.Segnale=='Short') & (df.Prezzo < df.Prezzo_exit)), 
    'target') )

… or this:

df['esito'] = ( np.where(
    ((df.Segnale=='Long') & (df.Prezzo > df.Prezzo_exit)) | ((df.Segnale=='Short') & (df.Prezzo < df.Prezzo_exit)), 
    'stop', 'target') )
Answered By: constantstranger

You need add parentheses to following comparison

(df['Prezzo'] < df['Prezzo_exit'])

For simplification, you can use np.select to select condition and choice in one statement.

Answered By: Ynjxsjmh
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.