applying functions to groups in pandas dataframe
dataframe, numpy, pandas, python
Solution
The problem is that `np.log2` cannot deal with the first column. Instead, you need to pass just your numeric column. You can do this as suggested in the comments, or define a `lambda`:
df.groupby('type').apply(lambda x: np.mean(np.log2(x['v'])))
As per comments, I would define a function:
df['w'] = [5, 6, 7,8]
def foo(x):
return x._get_numeric_data().apply(axis=0, func=np.log2).mean()
df.groupby('type').apply(foo)
# v w
# type
# X 0.000000 2.321928
# Y 1.528321 2.797439
Problem
I'm trying to apply simple functions to groups in pandas. I have this dataframe which I can group by `type`: ``` df = pandas.DataFrame({"id": ["a", "b", "c", "d"], "v": [1,2,3,4], "type": ["X", "Y", "Y", "Y"]}).set_index("id") df.groupby("type").mean() # gets the mean per type ``` I want to apply a function like `np.log2` only to the groups before taking the mean of each group. This does not work since `apply` is element wise and `type` (as well as potentially other columns in `df` in a real scenario) is not numeric: ``` # fails df.apply(np.log2).groupby("type").mean() ``` is there a way to apply `np.log2` only to the groups prior to taking the mean? I thought `transform` would be the answer but the problem is that it returns a dataframe that does not have the original `type` columns: ``` df.groupby("type").transform(np.log2) v id a 0.000000 b 1.000000 c 1.584963 d 2.000000 ``` Variants like grouping and then applying do not work: `df.groupby("type").apply(np.log2)`. What is the correct way to do this?