admin管理员组文章数量:1336293
Let's say I have DataFrame
s df1
and df2
:
>>> df1 = pd.DataFrame({'A': [0, 2, 4], 'B': [2, 17, 7], 'C': [4, 9, 11]})
>>> df1
A B C
0 0 2 4
1 2 17 9
2 4 7 11
>>> df2 = pd.DataFrame({'A': [9, 2, 32], 'B': [1, 3, 8], 'C': [6, 2, 41]})
>>> df2
A B C
0 9 1 6
1 2 3 2
2 32 8 41
What I want is the 3rd DataFrame
that will have minimal rows (min
is calculated based on column B
), that is:
>>> df3
A B C
0 9 1 6
1 2 3 2
2 4 7 11
I really don't want to do this by iterating over all rows and comparing them one by one, is there a faster and compact way to do this?
Let's say I have DataFrame
s df1
and df2
:
>>> df1 = pd.DataFrame({'A': [0, 2, 4], 'B': [2, 17, 7], 'C': [4, 9, 11]})
>>> df1
A B C
0 0 2 4
1 2 17 9
2 4 7 11
>>> df2 = pd.DataFrame({'A': [9, 2, 32], 'B': [1, 3, 8], 'C': [6, 2, 41]})
>>> df2
A B C
0 9 1 6
1 2 3 2
2 32 8 41
What I want is the 3rd DataFrame
that will have minimal rows (min
is calculated based on column B
), that is:
>>> df3
A B C
0 9 1 6
1 2 3 2
2 4 7 11
I really don't want to do this by iterating over all rows and comparing them one by one, is there a faster and compact way to do this?
Share asked Nov 19, 2024 at 18:19 k1r1t0k1r1t0 7671 gold badge5 silver badges20 bronze badges1 Answer
Reset to default 2You can mask
df1
with df2
when df2['B'] < df1['B']
:
out = df1.mask(df2['B'].lt(df1['B']), df2)
Output:
A B C
0 9 1 6
1 2 3 2
2 4 7 11
本文标签:
版权声明:本文标题:How to create DataFrame that is the minimal values based on 2 other DataFrame's in pandas (python)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742406684a2468964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论