Index pandas DataFrame by column numbers, when column names are integers

前端 未结 3 2133
遥遥无期
遥遥无期 2021-02-20 00:14

I am trying to keep just certain columns of a DataFrame, and it works fine when column names are strings:

In [2]: import numpy as np

In [3]: import pandas as pd         


        
3条回答
  •  生来不讨喜
    2021-02-20 00:42

    This is exactly the purpose of iloc, see here

    In [37]: df
    Out[37]: 
       10  11  12  13  14  15  16
    x   0   1   2   3   4   5   6
    y   7   8   9  10  11  12  13
    u  14  15  16  17  18  19  20
    z  21  22  23  24  25  26  27
    w  28  29  30  31  32  33  34
    
    In [38]: df.iloc[:,[1,3]]
    Out[38]: 
       11  13
    x   1   3
    y   8  10
    u  15  17
    z  22  24
    w  29  31
    

提交回复
热议问题