pandas groupby,您将获得一列的最大值和另一列的最小值 [英] pandas groupby where you get the max of one column and the min of another column

查看:233
本文介绍了pandas groupby,您将获得一列的最大值和另一列的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,如下所示:

I have a dataframe as follows:

user    num1    num2
a       1       1
a       2       2
a       3       3
b       4       4
b       5       5

我想要一个数据帧,该数据帧的每个用户的编号均应为num1起的最小值,每个用户的最大编号应为num2.

I want a dataframe which has the minimum from num1 for each user, and the maximum of num2 for each user.

输出应类似于:

user    num1    num2
a       1       3
b       4       5

我知道,如果我想同时获得两列的最大值,就可以这样做:

I know that if I wanted the max of both columns I could just do:

a.groupby('user')['num1', 'num2'].max()

是否存在一些等效项,而不必执行以下操作:

Is there some equivalent without having to do something like:

series_1 = a.groupby('user')['num1'].min() 
series_2 = a.groupby('user')['num2'].max()

# converting from series to df so I can do a join on user
df_1 = pd.DataFrame(np.array([series_1]).transpose(), index=series_1.index, columns=['num1']) 
df_2 = pd.DataFrame(np.array([series_2]).transpose(), index=series_2.index, columns=['num2'])

df_1.join(df_2)

推荐答案

使用 groupby + agg (dict),因此必须按subset reset_index 进行转换column.

Use groupby + agg by dict, so then is necessary order columns by subset or reindex_axis. Last add reset_index for convert index to column if necessary.

df = a.groupby('user').agg({'num1':'min', 'num2':'max'})[['num1','num2']].reset_index()
print (df)
  user  num1  num2
0    a     1     3
1    b     4     5

与什么相同:

df = a.groupby('user').agg({'num1':'min', 'num2':'max'})
                      .reindex_axis(['num1','num2'], axis=1)
                      .reset_index()
print (df)
  user  num1  num2
0    a     1     3
1    b     4     5

这篇关于pandas groupby,您将获得一列的最大值和另一列的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆