'Is it possible to loc a column based on a sort from another column

I have the following DataFrame, and I need to obtain only the values from Column 2 that go from 21 to 1, without losing the sorting from Column 1

Column1 Column2 Value
44508 20 A
44508 20 B
44509 21 B
44510 22 C
44511 1 A
44511 1 B
44512 2 A

This was my initial approach:

df= df.sort_values(by='Column1')
df = df.loc[(df['Column2'] >= 21) & (df['Column2'] <= 1)]

But it obviously didn't work as there won't be any value higher than 21 and lower than 1.

And I am trying to obtain this:

Column1 Column2 Value
44509 21 B
44510 22 C
44511 1 A

I also thought about first sorting the whole DataFrame, and then choose from the row where it is first found the 21 to the last time it is found the 1, but I could not anything about this approach, and I don't know if there might be a simpler solution to it.

PD: The Column 2 is a value generated based on column 1; I understand that a solution would be to just loc by column1 but I need it to be based on Column 2



Solution 1:[1]

Change your condition

df = df.loc[~((dfmerged['Column2'] < 21) & (df['Column2'] > 1))]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BENY