Home > other >  averaging across columns with dask
averaging across columns with dask

Time:11-14

I'm a total newbie with dataframes and really struggling with some basic ideas. I want to take the mean across several columns (x, y, and z motions to get the magnitude, for instance) for each row. Some dummy data:

'time', 'x', 'y', 'z'
1, 1.3, 0.1, 2.2   
2, 1.2, 0.5, 2.0    
3, 1.5, 0.3, 2.0  
4, 1.3, 0.0, 2.5 
5, 1.1, 0.3, 2.3

I thought I was onto something with groupby() but no dice.

grouped_df = df.groupby('x', 'y', 'z').mean()
print(grouped_df.head())

only prints the original dataframe, so I'm not sure what that function's even doing.

I'm at a total loss for how to move forward, any pointers appreciated!

CodePudding user response:

It seems that you don't really need groupby (the method name can be interpreted differently, but what it does is it groups rows based on the value of the passed columns). Instead, you are looking for .mean (without groupby):

cols_to_mean = ['x', 'y', 'z']
df['mean_of_cols'] = df[cols_to_mean].mean(axis=1)
df.head()
  • Related