0

I want to calculate some dataframe with other dataframes. When I make tihs calculation in excel, a complex formula shows up. How can I convert this calculation that is condition, to python code?

I have only dataframes which are create new dataframe what I want. I have 'Opened', 'Buy' and 'Sell' dataframes in my code and I want to get 'Closed' column by using dataframes which I have. I want to calculate 'Closed' column with pandas or differently.

I figure it out in excel with this formula:

  B2=IF(A2<=0;-MIN(ABS(A2);D2);MIN(A2;C2))
  B3=IF(A3<=0;-MIN(ABS(A3);D3);MIN(A3;C3))
  B4=IF(A4<=0;-MIN(ABS(A4);D4);MIN(A4;C4))
   ...



            A         B            C         D
   1      Opened   Closed         Buy       Sell
   2        8       8,00          9,30      1,50
   3      -12      -1,50          9,30      1,50
   4      -12     -11,50          4,00     11,50
   5        0       0,00          9,70     18,30
   6       -5      -5,00         55,50     57,80
   7      -15     -15,00         57,40     63,30
   8        7       7,0          14,60     12,60
   9      -12     -10,10         11,60     10,10



I got stuck at this stage. I hope, I can explain my problem well. I am waiting for your help.

1 Answer 1

1
df = pd.read_csv('data_1.csv', decimal=b',', dtype='float')

df

enter image description here

def decision(x):
    return np.where(x[0] <=0, -min(abs(x[0]), x[2]), min(x[0], x[1]))

df['Close'] = df.apply(lambda x: decision(x), axis=1)

df

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I am grateful to you.