rhz
rhz

Reputation: 1132

Select rows from a DataFrame based on ordered values in a column in pandas

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

Upvotes: 2

Views: 3092

Answers (2)

akuiper
akuiper

Reputation: 214957

You can put the values in a data frame, then do an inner join (merge by default) which should preserve the order on the left data frame:

D = pd.DataFrame({"D": [2, 0]})
D.merge(df)

#   D     A   B C
#0  2   bar one 1
#1  0   foo one 0

Or a more sure way to do this:

D.reset_index().merge(df).sort_values("index").drop("index", 1)
#   D     A   B C
#0  2   bar one 1
#1  0   foo one 0

Upvotes: 1

Scott Boston
Scott Boston

Reputation: 153460

df.loc[df['D'].isin([2,0])].sort_values(by='A')

Where 'A' can be any column the and ascending=False of you want reverse order.

Upvotes: 0

Related Questions