gpt4 book ai didi

python - 根据同一df中其他列的多个列值过滤df

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

我有以下 df:

id | status | id_reference
1 True NaN
4 False 1
2 False Nan
7 False 3
6 True 2
10 True 4

我怎样才能只过滤或想要 df 中的行,其中:

状态列Trueid_reference 列值作为值存在于 df 的另一行中,作为 id 列 中的值。

所以根据上面的 df,

我想要下面的行:

id | status | id_reference 
6 True 2
10 True 4

实际 df 要大得多,因为这是一个示例

最佳答案

id_reference 转换为 float(如果您还没有这样做)并使用 isin:

print (df.loc[df["status"]&df["id_reference"].astype(float).isin(df["id"])])

id status id_reference
4 6 True 2
5 10 True 4

关于python - 根据同一df中其他列的多个列值过滤df,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64057944/

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