简体   繁体   English

如何找到连续不同值的均值/中位数?

[英]How do I find the mean/median of the different values in a row?

I have a dataset in a csv file that looks like this: 我在csv文件中有一个数据集,如下所示:

teacher         student         student grade
Jon             marin           99
Jon             Rob             81
Jon             marly           90
Bon             martin          76
Bon             marie           56
Ton             Seri            43
Ton             Loku            99

I need an output that has the average of each teacher based on the grades obtained by student. 我需要一个输出,其中每个教师的平均值基于学生获得的成绩。 Which will look like this, 这将是这样的,

teacher         student         student grade       teacher Average
Jon             marin           99                  90
Jon             Rob             81                  90
Jon             marly           90                  90
Bon             martin          76                  66
Bon             marie           56                  66
Ton             Seri            43                  71
Ton             Loku            99                  71

What is the shortest possible way to get this ? 什么是最简单的方法来获得这个?

This is my approach but it does not seem to work. 这是我的方法,但它似乎不起作用。

import pandas as pd
df = pd.read_csv('test.csv', names=['teacher', 'student', 'student grade','Average'])
df.groupby('Star Rating').mean()

使用groupbytransform构建一个新列,该列的索引与正在分组的索引相同。

df['teacher average'] = df.groupby('teacher')['student grade'].transform('mean')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算 python 中的 PSD、中值频率和平均频率? - How do I calculate PSD, Median Frequency and Mean Frequency in python? 如何找到数据库中每行分组列的平均值 - How do I find the mean of each row of grouped columns in a database 如何在取列的平均值之前更新行值? - How do I update row values before taking the mean of the columns? 如何找到跨多个数据帧的总和值的平均值? - How do I find the mean of summed values across multiple Dataframes? 我如何找到平均值 - How do i find the mean 我如何在数据集上使用pandas找到中值? - How I do find median using pandas on a dataset? 我如何创建一个函数来查找高于中位数的平均值? - How do I create a function to find average above median? Python:如何在不使用 median() 调用的情况下读取数字的 .txt 文件以查找所述数字的中位数? - Python: How do I read a .txt file of numbers to find the median of said numbers without using median() call? 我如何计算这个 dataframe 中不同种类的中位数,中位数按种类标记并删除重复项 - how do I calculate the median of the different kinds in this dataframe with median labled by kind and duplicates removed 如何根据另一个值的中位数替换缺失值? - How do I replace missing values based on median of another value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM