I have a normal df
A = pd.DataFrame([[1, 5, 2], [2, 4, 4], [3, 3, 1], [4, 2, 2], [5, 1, 4]],
columns=['A', 'B', 'C'], index=[1, 2, 3, 4, 5])
Following this recipe, I got the the results I wanted.
In [62]: A.groupby((A['A'] > 2)).apply(lambda x: pd.Series(dict(
up_B=(x.B >= 0).sum(), down_B=(x.B < 0).sum(), mean_B=(x.B).mean(), std_B=(x.B).std(),
up_C=(x.C >= 0).sum(), down_C=(x.C < 0).sum(), mean_C=(x.C).mean(), std_C=(x.C).std())))
Out[62]:
down_B down_C mean_B mean_C std_B std_C up_B up_C
A
False 0 0 4.5 3.000000 0.707107 1.414214 2 2
True 0 0 2.0 2.333333 1.000000 1.527525 3 3
This approach is fine, but imagine you had to do this for a large number of columns (15-100), then you have to type all that stuff in the formula, which can be cumbersome.
Given that the same formulas are applied to ALL columns. Is there an efficient way to do this for a large number of columns?.
Thanks