python 3.x - A vectorized solution producing a new column in DataFrame that depends on conditions of existing columns and also the new column itself -
my current dataframe data follows:
df=pd.dataframe([[1.4,3.5,4.6],[2.8,5.4,6.4],[7.8,6.5,5.8]],columns=['t','i','m'])
t m 0 14 35 46 1 28 54 64 2 28 34 64 3 78 65 58
my goal apply vectorized operations on df conditions follows (pseudo code):
new column of answer starts value of 1.
for row in df.itertuples(): if (m > i) & (answer in row-1 odd number): answer in row = answer in row-1 + m elif (m > i): answer in row = answer in row-1 - m else: answer in row = answer in row-1
the desired output follows:
t m answer 0 14 35 46 1 1 28 54 59 60 2 78 12 58 2 3 78 91 48 2
any elegant solution appreciated.
Comments
Post a Comment