Reputation: 69
[
I have the below dataframe and I would like to return the average values of 'Age' and 'Sales' for each Flavor 'Chocolate' or 'Vanilla' so the average Age of 'Vanilla' is x, the average Age of 'Chocolate' is y, etc.
I haven't been able to find the answer anywhere on the web and I'm stuck.
print(MergeData.head())
Customer Type Flavor Age Sales Store Goals Goal FlavorCode \
0 1 Adult Chocolate 45 4.25 Greeley 25 25 C
1 2 Child Vanilla 5 2.90 Greeley 25 25 V
2 6 Teenager Chocolate 16 4.10 Greeley 25 25 C
3 8 Child Vanilla 4 3.00 Greeley 25 25 V
4 10 Child Vanilla 6 2.50 Greeley 25 25 V
AgeBin1 AgeBin2
0 (28.0, 72.0] B
1 (3.999, 14.0] A
2 (14.0, 28.0] A
3 (3.999, 14.0] A
4 (3.999, 14.0] A
Upvotes: 1
Views: 100
Reputation: 8826
you can even use df.loc
..
just using an example dataset here
>>> df
Name Score1 Score2
0 Alisa 62.2 89
1 Bobby 47.4 87
2 Cathrine 55.5 67
3 Madonna 74.6 55
4 Rocky 31.2 47
5 Sebastian 77.5 72
6 Jaqluine 85.6 76
7 Rahul 63.5 79
8 David 42.8 44
9 Andrew 32.3 92
10 Ajay 71.2 99
11 Teresa 57.4 69
>>> df.mean()
Score1 58.433333
Score2 73.000000
dtype: float64
>>> df.loc[:,"Score1":"Score2"].mean()
Score1 58.433333
Score2 73.000000
dtype: float64
Upvotes: 0
Reputation: 71620
IIUC:
df.groupby(['Flavor'])['Age','Sales'].transform('mean')
Demo:
print(df.groupby(['Flavor'])['Age','Sales'].transform('mean'))
Output:
Age Sales
0 30.5 4.175
1 5.0 2.800
2 30.5 4.175
3 5.0 2.800
4 5.0 2.800
Upvotes: 1