好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

在Python中按位置选择pandas

我只是试图通过整数访问命名的pandas列.

您可以使用df.ix [3]按位置选择行.

但如何按整数选择列?

我的数据帧:

df=pandas.DataFrame({'a':np.random.rand(5), 'b':np.random.rand(5)})
想到两种方法:

>>> df
          A         B         C         D
0  0.424634  1.716633  0.282734  2.086944
1 -1.325816  2.056277  2.583704 -0.776403
2  1.457809 -0.407279 -1.560583 -1.316246
3 -0.757134 -1.321025  1.325853 -2.513373
4  1.366180 -1.265185 -2.184617  0.881514
>>> df.iloc[:, 2]
0    0.282734
1    2.583704
2   -1.560583
3    1.325853
4   -2.184617
Name: C
>>> df[df.columns[2]]
0    0.282734
1    2.583704
2   -1.560583
3    1.325853
4   -2.184617
Name: C

编辑:原始答案建议使用df.ix [:,2],但现在不推荐使用此功能.用户应切换到df.iloc [:,2].

查看更多关于在Python中按位置选择pandas的详细内容...

  阅读:17次