查找 pandas 数据帧的最小索引,其中列值相等

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)

我有上面的数据框,我得到了一个 numpy id 数组 ids = np.array([100,212,219])

我想获取 ids 首次出现在 df

中的原始数据帧的索引

我想过在 indxmin 中使用 isin 函数,但不幸的是,它没有用。

我的输出应该是np.array([0,5,10])

试试 drop_duplicates

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