如何在()中进行选择

2024-06-11 17:56:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据源-一个来自数据库,另一个来自CSV文件。数据库就像一把钥匙。你知道吗

从CSV文件包含多个字段

Id  firstname  lastname
1   first1     last1
2   first2     last2
3   first3     last3

我希望对Pandas执行与SQL相同的操作:

select * from adr_tmp where id in(select id from xyz where key = ‘x’);

或者

delete from adr_tmp where id in(select id from xyz where key = ‘x’);

在此之后,我应该有一个新的数据帧与

Id  firstname  lastname
1   first1     last1
2   first2     last2

Tags: 文件csvfromid数据库firstnamewhereselect
1条回答
网友
1楼 · 发布于 2024-06-11 17:56:50

不是很难,你可以试试:

adr_temp[adr_temp['id'].isin(list(xyz['id'][xyz['key'] == 'x']))]

这将返回adr\ U temp的所有字段,其中“id”位于xyz的“id”列表中,其中“key”=“x”

对于“删除”,您只需选择上面的补充项:

adr_temp[~adr_temp['id'].isin(list(xyz['id'][xyz['key'] == 'x']))]

相关问题 更多 >