问题
I have the following dataframe:
g= pd.DataFrame({'A':[1,2,42,5,7],'B':[5,6,7,3,2]})
A B
0 1 5
1 2 6
2 42 7
3 5 3
4 7 2
am using the following list to filter the dataframe:
list_values = [5,7,1]
and get the following output using:
indexes = g[g['A'].isin(list_values)].index.values
output
array([0, 3, 4], dtype=int64)
How do I change the code so that indexes is the following?
array([3, 4, 0], dtype=int64)
Essentially, I am looking for a way to filter a DF with a list and return the original index values in the order of the filter list.
Thanks!
I looked at this but did not find what I was looking for: Select rows of pandas dataframe from list, in order of list
回答1:
You can use an ordered CategoricalDtype to enforce a custom sorting order. After sorting you return all indices for 5, then 7, then 1.
import pandas as pd
my_cat = pd.CategoricalDtype(categories=list_values, ordered=True)
#CategoricalDtype(categories=[5, 7, 1], ordered=True)
g.loc[g['A'].isin(list_values), 'A'].astype(my_cat).sort_values().index
#Int64Index([3, 4, 0], dtype='int64')
来源:https://stackoverflow.com/questions/60234180/how-to-maintain-original-dataframe-index-and-keep-order-of-input-list-when-using