Yyao
Yyao

Reputation: 413

How to use a list of values to select rows from a pandas Dataframe in specific order

If I have a pandas Dataframe like this:

>>> df = DataFrame({'A' : [5,6,3,4], 'B' : [1,2,3, 5]})
>>> df
     A   B
0    5   1
1    6   2
2    3   3
3    4   5

And I want to select some values in a list of values with specific order. It may look like this:

>>> df[df['A'].select_keeping_order([3, 4, 5])]
>>>
     A   B
2    3   3
3    4   5
0    5   1

I know there's a method called isin. But it selects the values in original order instead of "arguments order".

How can I do it?

Upvotes: 2

Views: 1634

Answers (1)

unutbu
unutbu

Reputation: 879471

In [134]: df = DataFrame({'A' : [5,6,3,4], 'B' : [1,2,3, 5]}, index=list('abcd'))

In [135]: df
Out[135]: 
   A  B
a  5  1
b  6  2
c  3  3
d  4  5

[4 rows x 2 columns]

In [138]: idx = pd.Index(df['A']).get_indexer([3,4,5]); idx
Out[138]: array([2, 3, 0])

In [136]: df.iloc[idx]
Out[136]: 
   A  B
c  3  3
d  4  5
a  5  1

[3 rows x 2 columns]

I changed df.index to make it clear that this method does not rely on the index being integers in incremental order.

Upvotes: 6

Related Questions