集体比与最小的合并在一起,同时保持整个数据框架

发布于 2025-01-30 09:34:00 字数 427 浏览 1 评论 0原文

我想结合组和最小值,但要保留整个数据框架。如果我使用以下方法,我最终只使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2025-02-06 09:34:00

最简单的方法是分两个步骤。首先准备包含这些最小值的支持数据框架。第二 - 初始数据框架与支持的内部合并。您可以像“内部联接”一样看待它,但不分开列(典型的联接需要您添加后缀以区分数据的源 - 左右)。

首先,我们创建我们的初始数据框架:
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')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文