Frank
Frank

Reputation: 67

How can i do this select in () / Pandas

I have two sources of data- One comes from the Database and one from a CSV File. The database comes just as a key.

From the CSV file contains more than one field

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

I'm looking to do same with Pandas as what I can do with SQL:

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

or

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

After this I should have a new data frame with

Id  firstname  lastname
1   first1     last1
2   first2     last2

Upvotes: 0

Views: 55

Answers (1)

Inox
Inox

Reputation: 2275

It's not very hard, you can try:

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

This will return all fields from adr_temp, where 'id' is in the list of 'id''s from xyz, where 'key' = 'x'

For delete, you can simply select the complementary of the above:

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

Upvotes: 2

Related Questions