python - How to perform a calculation involving values from multiple rows? -
i need compute value in dataframe @ time t involves values times t , t-1. in straight python, zip 2 length n-1 fraagments of list, so:
>>> x = [random.random() _ in range(10)] >>> x [0.09154030286701986, 0.7695293091436095, 0.003169617773302602, 0.18746852585939167, 0.16382872408720617, 0.951061080433954, 0.2880246300316386, 0.2585431567171105, 0.40819533123361884, 0.9482919441157496] >>> [(a - b) (a, b) in zip(x[:-1], x[1:])] [-0.6779890062765896, 0.7663596913703069, -0.18429890808608906, 0.023639801772185498, -0.7872323563467478, 0.6630364504023154, 0.029481473314528106, -0.14965217451650836, -0.5400966128821307] then handle border cases appropriate.
is there similar idiom using dataframes?
let me add together obvious (to me) analog doesn't i'd expect to:
in [321]: x out[321]: r1 r2 0 -1.059815 -1.159293 1 2.393336 0.458090 2 0.055295 1.389807 3 -0.102741 -0.126111 4 1.242702 -1.846763 5 -0.115352 -0.051099 6 -1.676272 1.117046 7 -0.404109 0.139790 in [322]: x["r1"][:-1] - x["r2"][1:] out[322]: 0 nan 1 1.935245 2 -1.334512 3 0.023370 4 3.089465 5 -0.064253 6 -2.793318 7 nan dtype: float64 i expected result of -1.059815 - 0.458090 in first position, not nan.
in [325]: [a-b (a, b) in zip(list(x["r1"])[:-1], list(x["r2"])[1:])] out[325]: [-1.5179054299355403, 1.0035286021021981, 0.1814061862111446, 1.7440218133011343, 1.2938016423931689, -1.2323982743162547, -1.8160616387411936]
take @ shift, name implies shifts values up/down index.
your approach doesn't work because pandas aligns index. when add together subsets of rows, aligned started because index isn't changed.
in [266]: x['r1'] - x['r2'].shift(-1) out[266]: 0 -1.517905 1 1.003529 2 0.181406 3 1.744022 4 1.293801 5 -1.232398 6 -1.816062 7 nan dtype: float64 python pandas
No comments:
Post a Comment