Keep a column with a categorical variable in Pandas with groupby and mean()(在Pandas中使用GROUPBY和Mean()保留带有类别变量的列)
本文介绍了在Pandas中使用GROUPBY和Mean()保留带有类别变量的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在groupby
和mean()
之后保留类别变量?
例如,给定数据帧df
:
ratio Metadata_A Metadata_B treatment
0 54265.937500 B10 1 AB_cmpd_01
11 107364.750000 B10 2 AB_cmpd_01
22 95766.500000 B10 3 AB_cmpd_01
24 64346.250000 B10 4 AB_cmpd_01
25 52726.333333 B10 5 AB_cmpd_01
30 65056.600000 B11 1 UT
41 78409.600000 B11 2 UT
52 133533.000000 B11 3 UT
54 102433.571429 B11 4 UT
55 82217.588235 B11 5 UT
60 89843.600000 B2 1 UT
71 98544.000000 B2 2 UT
82 179330.000000 B2 3 UT
84 107132.400000 B2 4 UT
85 73096.909091 B2 5 UT
我需要在Metadata_A
内取ratio
的平均值,但在末尾保留列treatment
:
理论上,类似于:
df.groupby(by='Metadata_A').mean().reset_index()
ratio Metadata_A Metadata_B treatment
0 54265.937500 B10 2.5 AB_cmpd_01
1 78409.600000 B11 2.5 UT
2 107132.400000 B2 2.5 UT
但是,平均化后treatment
列消失。
推荐答案
您可以将groupby
与agg
df.groupby(['Metadata_A','treatment'],as_index=False).agg({'Metadata_B':'mean','ratio':'first'})
Out[358]:
Metadata_A treatment Metadata_B ratio
0 B10 AB_cmpd_01 3 54265.9375
1 B11 UT 3 65056.6000
2 B2 UT 3 89843.6000
这篇关于在Pandas中使用GROUPBY和Mean()保留带有类别变量的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在Pandas中使用GROUPBY和Mean()保留带有类别变量的列


猜你喜欢
- 沿轴计算直方图 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- python-m http.server 443--使用SSL? 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01