Finding an array elements location in a pandas frame column (a.k.a pd.series)

前端 未结 5 1624
离开以前
离开以前 2021-01-12 02:27

I have a pandas frame similar to this one:

import pandas as pd
import numpy as np

data = {\'Col1\' : [4,5,6,7], \'Col2\' : [10,20,30,40], \'Col3\' : [100,5         


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 03:15

    You can use NumPy's in1d -

    df.index[np.in1d(df['Col4'],target_array)]
    

    Explanation

    1) Create a 1D mask corresponding to each row telling us whether there is a match between col4's element and any element in target_array :

    mask = np.in1d(df['Col4'],target_array)
    

    2) Use the mask to select valid indices from the dataframe as final output :

    out = df.index[np.in1d(df['Col4'],target_array)]
    

提交回复
热议问题