'How to do operetaions with columns based on date and column values?
I have this pandas Dataframe:
My goal is to perform some addictions and substractions based on culumns value conditions, and store the results inside a new column "pl",
This is the Dataframe I want to have:
The first non-NaN value will be necessarly in the "entry" column,
First scenario: I want that, if the next non-NaN value (after a non-NaN inside "entry" and then a non-NaN inside "tp1") is contained inside "tp2" column, then do this operation: (tp1 - entry) + (tp2 - entry)
Second scenario: I want that, if the next non-NaN value (after entry) is contained inside the column "sl1" then do this operation: sl1 - entry.
Third scenario: I want that, if the next non-NaN value (after entry) is contained inside the column "tp1" and there's a non-NaN value inside the column "sl2" then do this operation: tp1 - entry.
This is my code:
import pandas as pd
tbl = {"date" :["2022-02-27", "2022-02-27", "2022-02-27", "2022-02-27", "2022-02-27",
"2022-02-28", "2022-02-28","2022-02-28", "2022-02-28", "2022-02-01",
"2022-02-01", "2022-02-01", "2022-02-01"],
"entry" : ["NaN", "NaN", 1.2, "NaN", "NaN","NaN", 1.3, "NaN", "NaN", "NaN", 1.2, "NaN",
"NaN",],
"tp1" : ["NaN", "NaN", "NaN", 1.4, "NaN", "NaN", "NaN", "NaN", "NaN", "NaN", "NaN",
1.3, "NaN"],
"sl1" : ["NaN", "NaN", "NaN", "NaN", "NaN", "NaN", "NaN", "NaN", 1.15, "NaN", "NaN",
"NaN", "NaN"],
"tp2" : ["NaN", "NaN", "NaN", "NaN", 1.5, "NaN","NaN", "NaN", "NaN", "NaN", "NaN",
"NaN", "NaN"],
"sl2" : ["NaN", "NaN", "NaN", "NaN", "NaN", "NaN","NaN", "NaN", "NaN", "NaN", "NaN",
"NaN", 1.2]}
df = pd.DataFrame(tbl)
df = df.replace('NaN', float('nan'))
############## This is the way i'm trying to achive what i want:#########
#this code will only make tp1 - entry, or sl1 - entry, but it's wrong
#bacause it's made based on a dataframe without "sl2,tp2" consideration
group = df['date']
s1 = df['tp1'].fillna(df['sl1']).groupby(group).bfill()
s2 = df['entry'].groupby(group).bfill()
df.loc[~group.duplicated(), 'pl'] = s1-s2
I'm blocked here, I don't understand how to code the other conditions, Any ideas?
Edit The first value inside pl column is wrong, it should be 0.5. Not 0.20
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


