微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pandas - 在数据框中添加均值、最大值、最小值作为列

如何解决Pandas - 在数据框中添加均值、最大值、最小值作为列

我有一个df =

         statistics  s_values
year
1999  cigarette use       100
1999  cellphone use       310
1999   internet use       101
1999    alcohol use       100
1999       soda use       215
2000  cigarette use       315
2000  cellphone use       317
2000   internet use       325
2000    alcohol use       108
2000       soda use       200
2001  cigarette use       122
2001  cellphone use       311
2001   internet use       112
2001    alcohol use       144
2001       soda use       689

我根据 year indexstatistics column 计算了最大值、最小值和平均值。

我想在输出结果如下所示的数据框中插入平均值、最大值和最小值作为列

我想要的输出

         statistics   s_values        mean  min     max
year                    
1999    alcohol use     100.0        104.0  100.0   108.0
1999    cellphone use   310.0        313.5  310.0   317.0
1999    cigarette use   100.0        207.5  100.0   315.0
1999    internet use    101.0        213.0  101.0   325.0
1999    soda use        215.0        207.5  200.0   215.0
2000    alcohol use     108.0        104.0  100.0   108.0
2000    cellphone use   317.0        313.5  310.0   317.0
2000    cigarette use   315.0        207.5  100.0   315.0
2000    internet use    325.0        213.0  101.0   325.0
2000    soda use        200.0        207.5  200.0   215.0
2001    alcohol use     144.0        104.0  100.0   108.0
2001    cellphone use   311.0        313.5  310.0   317.0
2001    cigarette use   122.0        207.5  100.0   315.0
2001    internet use    112.0        213.0  101.0   325.0
2001    soda use        689.0        207.5  200.0   215.0   

我尝试执行以下操作,但列中的值都是 NaN

gen_mean = df.groupby('statistics').mean()
gen_min = df.groupby('statistics').min()
gen_max = df.groupby('statistics').max()

df.insert(2,'Gen Avg',gen_mean)
df.insert(3,'Gen Max',gen_max)
df.insert(4,'Gen Min',gen_min)

谢谢

解决方法

groupby(...).mean() 将返回一个包含与组对应的行的数据框。你需要transform

df['mean'] = df.groupby('statistics')['s_values'].transform('mean')
# I hope you get the idea how to get min/max
,

尝试使用 groupby aggregate + join

df = df.join(
    df.groupby('statistics')['s_values'].aggregate(['mean','min','max']),on='statistics'
)

df

         statistics  s_values        mean  min  max
year                                               
1999  cigarette use       100  179.000000  100  315
1999  cellphone use       310  312.666667  310  317
1999   internet use       101  179.333333  101  325
1999    alcohol use       100  117.333333  100  144
1999       soda use       215  368.000000  200  689
2000  cigarette use       315  179.000000  100  315
2000  cellphone use       317  312.666667  310  317
2000   internet use       325  179.333333  101  325
2000    alcohol use       108  117.333333  100  144
2000       soda use       200  368.000000  200  689
2001  cigarette use       122  179.000000  100  315
2001  cellphone use       311  312.666667  310  317
2001   internet use       112  179.333333  101  325
2001    alcohol use       144  117.333333  100  144
2001       soda use       689  368.000000  200  689

使用的帧:

df = pd.DataFrame({
    'year': [1999,1999,2000,2001,2001],'statistics': ['cigarette use','cellphone use','internet use','alcohol use','soda use','cigarette use','soda use'],'s_values': [100,310,101,100,215,315,317,325,108,200,122,311,112,144,689]
}).set_index('year')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?