Selecting pandas dataframe column by list

前端 未结 4 2014
闹比i
闹比i 2020-12-24 02:27

in one of my scripts I\'m selecting several columns of a dataframe, by a list of the column names. The following code works:

data = df[lst]

4条回答
  •  孤城傲影
    2020-12-24 03:08

    I think you need Index.intersection:

    df = pd.DataFrame({'A':[1,2,3],
                       'B':[4,5,6],
                       'C':[7,8,9],
                       'D':[1,3,5],
                       'E':[5,3,6],
                       'F':[7,4,3]})
    
    print (df)
       A  B  C  D  E  F
    0  1  4  7  1  5  7
    1  2  5  8  3  3  4
    2  3  6  9  5  6  3
    
    lst = ['A','R','B']
    
    print (df.columns.intersection(lst))
    Index(['A', 'B'], dtype='object')
    
    data = df[df.columns.intersection(lst)]
    print (data)
       A  B
    0  1  4
    1  2  5
    2  3  6
    

    Another solution with numpy.intersect1d:

    data = df[np.intersect1d(df.columns, lst)]
    print (data)
       A  B
    0  1  4
    1  2  5
    2  3  6
    

提交回复
热议问题