The question of selecting pandas dataframe rows on the basis of column values was addressed in:
Select rows from a DataFrame based on values in a column in pandas
without regard to ordering of the rows based on the order of the column values.
As an example, consider:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
'B': 'one one two three two two one three'.split(),
'C': np.arange(8), 'D': np.arange(8) * 2})
print(df.loc[df['D'].isin([0,2])])
yields:
A B C D
0 foo one 0 0
1 bar one 1 2
as does:
print(df.loc[df['D'].isin([2,0])])
where the order of the column values has been reversed.
I'd like to know how I can modify this expression to respect the order of the desired column values such that the output would be:
A B C D
1 bar one 1 2
0 foo one 0 0