I feel that I look almost everywhere, and I know that this is probably something very simple. I work with the pandas framework and want to fill / replace data in one of the columns based on the data from this TABLE. I tend to be more excel users and it is very simple in excel. If we have:
df = pd.DataFrame([0, -1, -1, -1, 0 , 0, 0, 1, 0]) df.columns = ['A'] df['B'] = df['A']
in excel, what I'm trying to do will be "= IF (AND (A2 = 0, B1 = -1), -1, A2) so that I can then drag the column" B ", and this applies In essence, based on the previous the data points of column B and the current value of column A, I need to update the current value of B.
I tried:
df['B'] = np.where((df['A'] == 0), (df['B'].shift(1) == -1), df['B'].replace(to_value = 0, method = 'ffill'), df['A'])
and many other versions of this, as well as variations of iterrows and other incredibly extreme workarounds to no avail.
Any suggestions are welcome.
EDIT:
the result will be:
df['B'] = [0, -1, -1, -1, -1 , -1, -1, 1, 0]