[英]Pandas DataFrame find the max after Groupby two columns and get counts
[英]Pandas Dataframe - find max between two columns
我有一个csv,看起来像这样:
Students | Math | Reading
Tom | 80 | 75
Mike | 65 | 90
我想导入此csv并确定哪个Student
的Math
和Reading
分数之间的差异最大。
在这个例子中。 我希望Mike
是他的结果,因为他的差异是25
而Tom的差异是15
。
您希望Student
成为数据框的索引。 有了您提供的样本数据,我将像这样导入它。
df = pd.read_csv('test.csv', sep='\s*\|\s*', engine='python', index_col=0)
当看到零个或多个空格,然后是竖线,然后零个或多个空格时,这将分隔列。 它还会将索引设置为“ Students
列
现在,您可以使用它来查找在各自的Math
和Reading
分数之间具有最大绝对差的Student
。
df.Math.sub(df.Reading).abs().idxmax()
'Mike'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.