集体比与最小的合并在一起,同时保持整个数据框架
我想结合组和最小值,但要保留整个数据框架。如果我使用以下方法,我最终只使用2列,Col1和Col2:
此DF:
col1 col2 col3
1 1 'A'
1 0 'B'
2 2 'C'
2 3 'D'
DF.Groupby(Df ['col1'])[['col2']]。
col1 col2
1 0
2 2
对于 确定了Col2的最小行,我想要Col3的该行的相应元素,因此:
col1 col2 col3
1 0 'B'
2 2 'C'
I would like to combine groupby and min but keep the entire dataframe. If I use the below approach I end up with only the 2 columns, the col1 and col2:
For this df:
col1 col2 col3
1 1 'A'
1 0 'B'
2 2 'C'
2 3 'D'
df.groupby(df['col1'])[['col2']].min():
col1 col2
1 0
2 2
But instead once the min row of col2 is identified, I want the corresponding elements of that row from col3, so this:
col1 col2 col3
1 0 'B'
2 2 'C'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是分两个步骤。首先准备包含这些最小值的支持数据框架。第二 - 初始数据框架与支持的内部合并。您可以像“内部联接”一样看待它,但不分开列(典型的联接需要您添加后缀以区分数据的源 - 左右)。
首先,我们创建我们的初始数据框架:
df1 = pd.dataframe(data = {'col1':[1,1,2,2],'col2':[1,0,2,3],'col3':['a'',' b','c','d']})
然后我们必须执行我们的组。事后我们必须重置索引。否则
col1
将被视为索引:df2 = df.groupby(df ['col1'])[['col2']]。min()。reset_index()
,我们的最后一步是内部合并两个:
pd.merge(df1,df2,how ='inneR')
The easiest way to do this would be in two steps. First to prepare a support dataframe that contains these minimal values. Second - internal merge of the initial dataframe with the supporting one. You can look at it like "inner join" but without separating the columns (typical join would require you to add suffixes to differentiate between the sources of the data - left and right).
First we create our initial dataframe:
df1 = pd.DataFrame(data={'col1':[1,1,2,2],'col2':[1,0,2,3],'col3':['A','B','C','D']})
Then we have to perform our groupby. We have to reset the index after the fact. Otherwise column
col1
will be treated as an index:df2 = df.groupby(df['col1'])[['col2']].min().reset_index()
And our last step is to merge both internally:
pd.merge(df1, df2, how='inner')