I have 3 columns (id, price and signal). The signal column consists of "Buy", "Sell" and "none" strings. My goal is to make a fourth column called signal2 where all the none strings will be replaced. There rule hereby should be: If the string before the none value is == "Buy", then change it to "Buy". If it is == "none" then check the next value.
code looks like this:
import numpy as np
import pandas as pd
df = pd.DataFrame(
{
"id": [0, 1, 2, 3, 4,5],
"price": [26,23,23,22,25,23],
"signal": ["Buy", "none", "none", "Sell", "none", "Buy"],
}
)
print(df)
This is how the data should look like at the end:
id price signal signal2
0 0 26 Buy Buy
1 1 23 none Buy
2 2 23 none Buy
3 3 22 Sell Sell
4 4 25 none Sell
5 5 23 Buy Buy