Selecting multiple columns in a pandas dataframe

后端 未结 19 1782
醉话见心
醉话见心 2020-11-22 00:08

I have data in different columns but I don\'t know how to extract it to save it in another variable.

index  a   b   c
1      2   3   4
2      3   4   5
         


        
19条回答
  •  感情败类
    2020-11-22 00:24

    I've seen several answers on that, but on remained unclear to me. How would you select those columns of interest? The answer to that is that if you have them gathered in a list, you can just reference the columns using the list.

    Example

    print(extracted_features.shape)
    print(extracted_features)
    
    (63,)
    ['f000004' 'f000005' 'f000006' 'f000014' 'f000039' 'f000040' 'f000043'
     'f000047' 'f000048' 'f000049' 'f000050' 'f000051' 'f000052' 'f000053'
     'f000054' 'f000055' 'f000056' 'f000057' 'f000058' 'f000059' 'f000060'
     'f000061' 'f000062' 'f000063' 'f000064' 'f000065' 'f000066' 'f000067'
     'f000068' 'f000069' 'f000070' 'f000071' 'f000072' 'f000073' 'f000074'
     'f000075' 'f000076' 'f000077' 'f000078' 'f000079' 'f000080' 'f000081'
     'f000082' 'f000083' 'f000084' 'f000085' 'f000086' 'f000087' 'f000088'
     'f000089' 'f000090' 'f000091' 'f000092' 'f000093' 'f000094' 'f000095'
     'f000096' 'f000097' 'f000098' 'f000099' 'f000100' 'f000101' 'f000103']
    

    I have the following list/numpy array extracted_features, specifying 63 columns. The original dataset has 103 columns, and I would like to extract exactly those, then I would use

    dataset[extracted_features]
    

    And you will end up with this

    This something you would use quite often in Machine Learning (more specifically, in feature selection). I would like to discuss other ways too, but I think that has already been covered by other stackoverflowers. Hope this've been helpful!

提交回复
热议问题