gpt4 book ai didi

python - 基于列的 2 个数据框之间的减法

转载 作者:行者123 更新时间:2023-12-02 02:18:28 25 4
gpt4 key购买 nike

我有 2 个数据框 pandas df1、df2:

df1 = pd.DataFrame({'col1': ['A', 'B', 'C', 'D'],
'col2': ["D1","D2","D3","D4"],
'col3': ["C1","C2","C3","C4"],
'col4': ["B1","B2","B3","B4"]})


df2 = pd.DataFrame({'col_ID': ['A', 'D']})


df1=
col1 | col2 | col3 | col4
A | D1 | C1 | B1
B | D2 | C2 | B2
C | D3 | C3 | B3
D | D4 | C4 | B4

和 df2 :

df2=
col_ID |
A |
D |

我想要 df1.col1 中 df2.col_ID 中不存在的行

df1=
col1 | col2 | col3 | col4
B | D2 | C2 | B2
C | D3 | C3 | B3

感谢您的帮助

最佳答案

isin

df1[~df1.col1.isin(df2.col_ID)]

关于python - 基于列的 2 个数据框之间的减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66806753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com