How to maintain original dataframe index and keep order of input list when using isin()?

a 夏天 提交于 2020-03-25 22:04:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!