Given a dataframe with the following format:
TEST_ID | ATOMIC_NUMBER | COMPOSITION_PERCENT | POSITION
1 | 28 | 49.84 | 0
1 | 22 | 50.01 | 0
1 | 47 | 0.06 | 1
2 | 22 | 49.84 | 0
2 | 47 | 50.01 | 1
3 | 28 | 49.84 | 0
3 | 22 | 50.01 | 0
3 | 47 | 0.06 | 0
I want to select only the tests that have ATOMIC_NUMBER of 22 AND 28 in POSITION 0, no more no less. So I'd like a filter that returns:
TEST_ID | ATOMIC_NUMBER | COMPOSITION_PERCENT | POSITION
1 | 28 | 49.84 | 0
1 | 22 | 50.01 | 0
1 | 47 | 0.06 | 1
EDIT: I'm trying to convert this logic from SQL into python. Here's the SQL code:
select * from compositions
where compositions.test_id in (
select a.test_id from (
select test_id from compositions
where test_id in (
select test_id from (
select * from COMPOSITIONS where position == 0 )
group by test_id
having count(test_id) = 2 )
and atomic_number = 22) a
join (
select test_id from compositions
where test_id in (
select test_id from (
select * from COMPOSITIONS where position == 0 )
group by test_id
having count(test_id) = 2 )
and atomic_number = 28) b
on a.test_id = b.test_id )