根据行值返回列的平均值

时间:2018-10-08 02:38:56

标签: python dataframe

[2]

我有下面的数据框,我想返回每种口味“巧克力”或“香草”的“年龄”和“销售”的平均值,因此“香草”的平均年龄是x,即“巧克力”是y,等等。

我无法在网络上的任何地方找到答案,我被困住了。

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  

2 个答案:

答案 0 :(得分:1)

IIUC:

df.groupby(['Flavor'])['Age','Sales'].transform('mean')

演示:

print(df.groupby(['Flavor'])['Age','Sales'].transform('mean'))

输出:

    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

答案 1 :(得分:0)

您甚至可以使用df.loc ..

仅在此处使用示例数据集

>>> 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

dataFrame的平均值..

>>> df.mean()
Score1    58.433333
Score2    73.000000
dtype: float64

对于特定列:

>>> df.loc[:,"Score1":"Score2"].mean()
Score1    58.433333
Score2    73.000000
dtype: float64