在Pandas Dataframe中按分组进行多重聚合

编程入门 行业动态 更新时间:2024-10-28 16:24:02
本文介绍了在Pandas Dataframe中按分组进行多重聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 SQL : Select Max(A) , Min (B) , C from Table group by C

我想在数据框上的熊猫中执行相同的操作.我离这更近了:

I want to do the same operation in pandas on a dataframe. The closer I got was till :

DF2= DF1.groupby(by=['C']).max()

我在两列中最多获取的地方,在分组时如何做一个以上的操作.

where I land up getting max of both the columns , how do i do more than one operation while grouping by.

推荐答案

尝试agg()函数:

import numpy as np import pandas as pd df = pd.DataFrame(np.random.randint(0,5,size=(20, 3)), columns=list('ABC')) print(df) print(df.groupby('C').agg({'A': max, 'B':min}))

输出:

A B C 0 2 3 0 1 2 2 1 2 4 0 1 3 0 1 4 4 3 3 2 5 0 4 3 6 2 4 2 7 3 4 0 8 4 2 2 9 3 2 1 10 2 3 1 11 4 1 0 12 4 3 2 13 0 0 1 14 3 1 1 15 4 1 1 16 0 0 0 17 4 0 1 18 3 4 0 19 0 2 4 A B C 0 4 0 1 4 0 2 4 2 3 0 4 4 0 1

或者,您可能要检查 pandas.read_sql_query()功能...

Alternatively you may want to check pandas.read_sql_query() function...

更多推荐

在Pandas Dataframe中按分组进行多重聚合

本文发布于:2023-11-22 00:37:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Pandas   Dataframe

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!