pandas -按一列分组,按另一列排序,从第三列获取价值 [英] Pandas - group by one column, sort by another, get value from the third column

查看:139
本文介绍了 pandas -按一列分组,按另一列排序,从第三列获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取pandas数据框,按一列对其进行分组,按另一列对其进行排序,然后从第三列中获取第一个元素,然后填充原始数据框.

I would like to take pandas dataframe, group it by one column, sort it by another column and take first element from third column and populate original dataframe.

这是我的原始df.我要按col_1分组,按col_2排序(升序),并从col_3提取第一个元素,然后将结果填充到col_4中.

Here is my original df. I would to group by col_1, sort by col_2 (ascending) and take first element from col_3 and populate col_4 with results.

df_in = pd.DataFrame({'col_1':['A', 'A', 'A', 'B', 'B', 'B'], 'col_2': [5,9,2, 3,7,1],
                   'col_3': ['c','d','k','n','l','f']})

[

以下是输出df的外观:

Here is how output df should look like:

df_out = pd.DataFrame({'col_1':['A', 'A', 'A', 'B', 'B', 'B'], 'col_2': [5,9,2, 3,7,1],
                   'col_3': ['c','d','k','n','l','f'], 'col_4': ['k','k','k','f','f','f'], })

我可以通过分组和变换来完成分组和排序,但是如何提取第一个元素尚不清楚.

I can accomplish grouping and sorting with group and transform, but how to extract first element is not clear.

抱歉,SO无法正确显示图像;-(

Sorry SO does not display images correctly ;-(

推荐答案

 df['col_4']=df.sort_values(['col_1','col_2']).groupby('col_1')['col_3'].transform(lambda x: x.iloc[0])

输出:

  col_1  col_2 col_3 col_4
0     A      5     c     k
1     A      9     d     k
2     A      2     k     k
3     B      3     n     f
4     B      7     l     f
5     B      1     f     f

这篇关于 pandas -按一列分组,按另一列排序,从第三列获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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