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

前端 未结 5 1631
离开以前
离开以前 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:12

    import pandas as pd
    import numpy as np
    
    data = {'Col1' : [4,5,6,7], 'Col2' : [10,20,30,40], 'Col3' : [100,50,-30,-50], 'Col4' : ['AAA', 'BBB', 'AAA', 'CCC']}
    target_array = np.array(['AAA', 'CCC', 'EEE'])
    
    df = pd.DataFrame(data=data, index = ['R1','R2','R3','R4'])
    
    df['in_col'] = df['Col4'].apply(lambda x: x in target_array)
    

    Is this what you were looking for? Then you can groupby the new column and query the True elements.

提交回复
热议问题