I am having difficulty creating an IF statement that does the following:
Here's a tidy way to do it using Pandas: Swap all the NaN for empty strings, and return whatever string value is in each row. If a row is empty, return what came before it.
import pandas as pd
def decide(data):
if len(data.sum()):
return data.sum()
return decide(df.iloc[data.name - 1])
df.fillna("", inplace=True)
df.apply(decide, axis=1)
Output:
index
0 Buy
1 Buy
2 Sell
3 Sell
4 Buy
5 Sell
6 Sell
7 Sell
8 Sell
9 Buy
10 Sell
dtype: object
Note: Making a couple of assumptions here. First, assuming only Buy or Sell occurs in a row. Second, assuming first row is not empty.
Data:
df = pd.read_clipboard(index_col="index") # copied from OP