2017-05-28 4 views
1

Ich habe einen Pandas-Index und eine Liste von ganzzahligen Positionen in diesem Index, die möglicherweise nicht alle in diesem Index existieren. Z.B.Weiche Auswahl für Pandas Index

idx = pd.Index(['A', 'B', 'C']) 

l1 = np.array([1, 2]) 
l2 = np.array([1, 4]) 

Jetzt

idx.loC# doesn't exist 
idx[l1] # => ['B', 'C'] 
idx[l2] # KeyError 

Wie erhalte ich

idx.magic(l2) # => ['B'] 

Gerade jetzt etwas zu tun, ich bin etwa wie

pd.DataFrame(idx, columns=['name']).merge(pd.DataFrame(l2), how='left', left_on='name', right_on...) 

die Brutto ist.

Antwort

2

Soweit ich weiß Index keine speziellen Lookups für diesen Fall liefert, aber man konnte einfach den Indexer filtern, wie folgt aus:

l2 = l2[l2 < len(idx)] 

idx[l2] 
Out[15]: Index(['B'], dtype='object')