Shew
Shew

Reputation: 1596

Finding the smallest indices of a pandas datafrmae where column value equality holds

data = """
id,name
100,A
100,B
101,C
101,D
101,pp;
212,E
212,F
215,ds
215,G
215,trtr
219, dsds
219, sas
219, dasa
300,Endüstrisi`
"""

df = pd.read_csv(StringIO(data))
df = pd.concat([df]*5)

I have the above dataframe and I am given a numpy array of ids as ids = np.array([100,212,219])

I want to get the indices of the original dataframe where the ids first appears in df

I thought of using isin function with indxmin, but unfortunately, it didn't work.

My output should be np.array([0,5,10])

Upvotes: 1

Views: 26

Answers (1)

BENY
BENY

Reputation: 323316

Try with drop_duplicates

s = df.drop_duplicates('id')
out = s[s['id'].isin(ids)].index.values
Out[168]: array([ 0,  5, 10], dtype=int64)

Upvotes: 1

Related Questions