pandas - read and write array at the same time python -
i want recalculate column "a" of given dataframe = df. way of doing not fill in new, calculated values on old ones.
import pandas pd import numpy np numpy.random import randn df = pd.dataframe(randn(100)) df["a"] = np.nan df["b"] = randn() df.a[0] = 0.5 df.a= df.a.shift(1) * df.b do have ideas how can fill solve that?
i want calculate "a" depending on previous value "b":
b 0.5 2 #set starting value df.a[0] = 0.5 since there no value prior that, there's no calculation performed. 1.5 3 # = previous value of *b (0.5*3) =1.5 15 10 # = previous value of *b (1.5*10) =15 45 3 # = previous value of *b (15*3) =45 the problem calcation not perform / results of calcutation not overwrite set values.
how this?
df = pd.dataframe({'a': [none] * 4, 'b': [2, 3, 10, 3]}) df.a.iloc[0] = 0.5 df.a.iloc[1:] = (df.b.shift(-1).cumprod() * df.a.iat[0])[:-1].values >>> df b 0 0.5 2 1 1.5 3 2 15 10 3 45 3
Comments
Post a Comment