Pandas:计算数据框中重复条目的平均值 [英] Pandas: calculating the mean values of duplicate entries in a dataframe
本文介绍了Pandas:计算数据框中重复条目的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在使用 Python 和 Pandas 中的数据框,该数据框在第一列中包含重复的条目.数据框看起来像这样:
I have been working with a dataframe in python and pandas that contains duplicate entries in the first column. The dataframe looks something like this:
sample_id qual percent
0 sample_1 10 20
1 sample_2 20 30
2 sample_1 50 60
3 sample_2 10 90
4 sample_3 100 20
我想写一些东西来识别第一列中的重复条目并计算后续列的平均值.理想的输出类似于以下内容:
I want to write something that identifies duplicate entries within the first column and calculates the mean values of the subsequent columns. An ideal output would be something similar to the following:
sample_id qual percent
0 sample_1 30 40
1 sample_2 15 60
2 sample_3 100 20
我整个下午都在为这个问题苦苦挣扎,希望得到任何帮助.
I have been struggling with this problem all afternoon and would appreciate any help.
推荐答案
groupby
sample_id
列并使用 mean
df.groupby('sample_id').mean().reset_index()
或df.groupby('sample_id', as_index=False).mean()
给你
这篇关于Pandas:计算数据框中重复条目的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文