You can simply:
df['avg'] = df.mean(axis=1)
Monday Tuesday Wednesday avg
Mike 42 NaN 12 27.000000
Jenna NaN NaN 15 15.000000
Jon 21 4 1 8.666667
because .mean() ignores missing values by default: see docs.
To select a subset, you can:
df['avg'] = df[['Monday', 'Tuesday']].mean(axis=1)
Monday Tuesday Wednesday avg
Mike 42 NaN 12 42.0
Jenna NaN NaN 15 NaN
Jon 21 4 1 12.5
Answer from Stefan on Stack OverflowYou can simply:
df['avg'] = df.mean(axis=1)
Monday Tuesday Wednesday avg
Mike 42 NaN 12 27.000000
Jenna NaN NaN 15 15.000000
Jon 21 4 1 8.666667
because .mean() ignores missing values by default: see docs.
To select a subset, you can:
df['avg'] = df[['Monday', 'Tuesday']].mean(axis=1)
Monday Tuesday Wednesday avg
Mike 42 NaN 12 42.0
Jenna NaN NaN 15 NaN
Jon 21 4 1 12.5
Alternative - using iloc (can also use loc here):
df['avg'] = df.iloc[:,0:2].mean(axis=1)
python - pandas: return average of multiple columns - Stack Overflow
How to do a simple rolling average across multiple columns in pandas?
What have you tried so far?
More on reddit.compython - Pandas dataframe: Group by two columns and then average over another column - Stack Overflow
pandas - How to find median/average values between data frames with slightly different columns? - Data Science Stack Exchange
Given this dataframe:
df = pd.DataFrame({
"Gender": ["Male", "Female", "Female", "Male"],
"Age": [28, 40, 23, 35],
"Salary": [45000, 70000, 40000, 55000],
"Yr_exp": [6, 15, 1, 12]
})
df
Age Gender Salary Yr_exp
0 28 Male 45000 6
1 40 Female 70000 15
2 23 Female 40000 1
3 35 Male 55000 12
Group by gender and use the mean() function:
df.groupby("Gender").mean()
Age Salary Yr_exp
Gender
Female 31.5 55000.0 8.0
Male 31.5 50000.0 9.0
Edit: you may need to change the way you're indexing after groupby(): df['Age', 'Salary'] gives a KeyError, but df[['Age', 'Salary']] returns the expected:
Age Salary
0 28 45000
1 40 70000
2 23 40000
3 35 55000
Try changing
df.groupby("Gender", as_index=True)['Age', 'Salary', 'Yr_exp'].mean()
to
df.groupby("Gender", as_index=True)[['Age', 'Salary', 'Yr_exp']].mean()
You can also use pandas.agg():
df.groupby("Gender").agg({'Age' : 'mean', 'Salary' : 'mean', 'Yr_exp': 'mean'})
Would result to:
Age Salary Yr_exp
Gender
Female 31.5 55000 8
Male 31.5 50000 9
Using .agg() give you the chance to apply different functions to a grouped object - something like:
df.groupby("Gender").agg({'Age' : 'mean', 'Salary' : ['min', 'max'], 'Yr_exp': 'sum'})
Outputs:
Age Salary Yr_exp
mean min max sum
Gender
Female 31.5 40000 70000 16
Male 31.5 45000 55000 18
I'm having trouble creating a table that has a rolling average with a 3 month window for it. This is kind of what I have right now:
Date A B 2020-3-1 10 2 2020-2-1 2 3 2020-1-1 4 1 2019-12-1 6 8 2019-11-1 2 4
The date column all have days set on the first of the month because the datas been grouped by so I only get essentially year-month.
Then end result I would like to have looks like this:
Date A B 2020-3-1 5.3 2 2020-2-1 4 3 2020-1-1 4 4.3 2019-12-1 NAN NAN 2019-11-1 NAN NAN
You need to pass a list of the columns to groupby, what you passed was interpreted as the axis param which is why it raised an error:
In [30]:
columns = ['col1','col2','avg']
df = pd.DataFrame(columns=columns)
df.loc[0] = [1,2,3]
df.loc[1] = [1,3,3]
print(df[['col1','col2','avg']].groupby(['col1','col2']).mean())
avg
col1 col2
1 2 3
3 3
If you want to group by multiple columns, you should put them in a list:
columns = ['col1','col2','value']
df = pd.DataFrame(columns=columns)
df.loc[0] = [1,2,3]
df.loc[1] = [1,3,3]
df.loc[2] = [2,3,1]
print(df.groupby(['col1','col2']).mean())
Or slightly more verbose, for the sake of getting the word 'avg' in your aggregated dataframe:
import numpy as np
columns = ['col1','col2','value']
df = pd.DataFrame(columns=columns)
df.loc[0] = [1,2,3]
df.loc[1] = [1,3,3]
df.loc[2] = [2,3,1]
print(df.groupby(['col1','col2']).agg({'value': {'avg': np.mean}}))