library(data.table)
test = data.table(x=seq(10, 20), y=seq(30, 50, 2), z=seq(40, 50))
test[, new := sum(x + y), by=(x + y) %% 2]
test
x y z new 1: 10 30 40 260 2: 11 32 41 275 3: 12 34 42 260 4: 13 36 43 275 5: 14 38 44 260 6: 15 40 45 275 7: 16 42 46 260 8: 17 44 47 275 9: 18 46 48 26010: 19 48 49 275
test = pd.DataFrame({'x': range(10, 20), 'y': range(30, 50, 2), 'z': range(40, 50)})test = (test.assign(temp = lambda x: (x.x + x.y) % 2) .pipe(lambda df: df.groupby('temp') .apply(lambda x: pd.Series({'new': np.sum(x.x + x.y)})) .merge(df, left_index=True, right_on='temp')) .drop('temp', axis=1))test
new x y z 0 260 10 30 40 2 260 12 34 42 4 260 14 38 44 6 260 16 42 46 8 260 18 46 48 1 275 11 32 41 3 275 13 36 43 5 275 15 40 45 7 275 17 44 47 9 275 19 48 49
--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.