基于行值返回列的平均值

2024-05-14 01:10:10 发布

您现在位置:Python中文网/ 问答频道 /正文

[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  

Tags: 数据答案child地方平均值print年龄香草
2条回答

你甚至可以使用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

数据帧的平均值

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

对于特定列:

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

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

相关问题 更多 >