Home > Blockchain >  excel if and logic to data frame
excel if and logic to data frame

Time:07-18

I have aloe of excel files I am trying to convert to python codes and need some help :) I have a data frame like this:

Date    STD-3   STD-25  STD-2   STD-15  STD-1   Data    STD1    STD15   STD2    STD25   STD3
11.05.2022  -0,057406797    -0,047838998    -0,038271198    -0,028703399    -0,019135599    0,021233631 0,019135599 0,028703399 0,038271198 0,047838998 0,057406797

I need to check for this logic:

"Data" < "STD1" and "Data" > "STD-1" = 0

"Data" > "STD1" and "Data" < "STD15" = 1

"Data" > "STD15" and "Data" < "STD2" = 1,5

"Data" > "STD2" and "Data" < "STD25" = 2

"Data" > "STD25" and "Data" < "STD3" = 2,5

"Data" > "STD3" = 3

"Data" < "STD-1" and "Data" > "STD-15" = -1

"Data" < "STD-15" and "Data" > "STD-2" = -1,5

"Data" < "STD-2" and "Data" > "STD-25" = -2

"Data" < "STD-25" and "Data" > "STD-3" = -2,5

"Data" > "STD3" = -3

And add the output to a new column.

CodePudding user response:

condition = [((df['DATA'] < df['STD1']) & (df['DATA'] > df['STD-1'])), ((df['DATA'] > df['STD1']) & (df['DATA'] < df['STD15'])), ((df['DATA'] > df['STD15']) & (df['DATA'] < df['STD2'])), ((df['DATA'] > df['STD2']) & (df['DATA'] < df['STD25'])), ((df['DATA'] > df['STD25']) & (df['DATA'] < df['STD3'])), df['DATA'] > df['STD3'], ((df['DATA'] < df['STD-1']) & (df['DATA'] > df['STD-15'])), ((df['DATA'] < df['STD-15']) & (df['DATA'] > df['STD-2'])), ((df['DATA'] < df['STD-25']) & (df['DATA'] > df['STD-3'])), df['DATA'] > df['STD-3']]
result = [0, 1, 1.5, 2, 2.5, 3, -1, -1.5, -2.5, -3]

df['RESULT'] = np.select(condition, result, None)
  • Related