pandas get position of a given index in DataFrame

For your first question:

base = df.index.get_indexer_for((df[df.A == 2].index))

or alternatively

base = df.index.get_loc(18)

To get the surrounding ones:

mask = pd.Index(base).union(pd.Index(base - 1)).union(pd.Index(base + 1))

I used Indexes and unions to remove duplicates. You may want to keep them, in which case you can use np.concatenate

Be careful with matches on the very first or last rows 🙂

Leave a Comment