我有以下 df:
id | status | id_reference
1 True NaN
4 False 1
2 False Nan
7 False 3
6 True 2
10 True 4
我怎样才能只过滤或想要 df 中的行,其中:
状态列为True且id_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/