153

I'm simply trying to access named pandas columns by an integer.

You can select a row by location using df.ix[3].

But how to select a column by integer?

My dataframe:

df=pandas.DataFrame({'a':np.random.rand(5), 'b':np.random.rand(5)})
smci
  • 32,567
  • 20
  • 113
  • 146
Jason Strimpel
  • 14,670
  • 21
  • 76
  • 106

7 Answers7

228

Two approaches that come to mind:

>>> 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

Edit: The original answer suggested the use of df.ix[:,2] but this function is now deprecated. Users should switch to df.iloc[:,2].

stklik
  • 834
  • 1
  • 8
  • 19
DSM
  • 342,061
  • 65
  • 592
  • 494
  • 30
    FYI df.ix is now replaced with df.iloc – Yonas Kassa Oct 26 '17 at 12:58
  • Note that if you have two columns with the same name df.iloc[:,2] method works, returning just one column but df[df.columns[2]] method will return both columns with the same name. – BobbyG May 03 '19 at 21:07
  • As BobbyG directly above correctly states in case of duplicate column names df.columns[2] then df[df.columns[2]] will return all columns of that name and is a dataframe, not a series object. – Uwe Mayer Nov 12 '20 at 21:07
66

You can also use df.icol(n) to access a column by integer.

Update: icol is deprecated and the same functionality can be achieved by:

df.iloc[:, n]  # to access the column at the nth position
ayhan
  • 70,170
  • 20
  • 182
  • 203
Adrian
  • 1,837
  • 1
  • 20
  • 21
  • 4
    Note that for the upcoming version 0.11.0, these methods are deprecated and may be removed in future versions. See http://pandas.pydata.org/pandas-docs/dev/indexing.html#indexing-integer on how to select by position using iloc/iat. – Wouter Overmeire Apr 12 '13 at 10:04
  • 1
    The above link is deprecated because the indexing docs have since been restructured: http://pandas.pydata.org/pandas-docs/stable/indexing.html#selection-by-position. As of today, in which the most recent version is 0.21.0, `iloc` remains the documented approach to accessing a column by position. – iff_or Jun 15 '17 at 00:54
  • how to select by a list of column numbers? – thistleknot Apr 27 '21 at 23:32
27

You could use label based using .loc or index based using .iloc method to do column-slicing including column ranges:

In [50]: import pandas as pd

In [51]: import numpy as np

In [52]: df = pd.DataFrame(np.random.rand(4,4), columns = list('abcd'))

In [53]: df
Out[53]: 
          a         b         c         d
0  0.806811  0.187630  0.978159  0.317261
1  0.738792  0.862661  0.580592  0.010177
2  0.224633  0.342579  0.214512  0.375147
3  0.875262  0.151867  0.071244  0.893735

In [54]: df.loc[:, ["a", "b", "d"]] ### Selective columns based slicing
Out[54]: 
          a         b         d
0  0.806811  0.187630  0.317261
1  0.738792  0.862661  0.010177
2  0.224633  0.342579  0.375147
3  0.875262  0.151867  0.893735

In [55]: df.loc[:, "a":"c"] ### Selective label based column ranges slicing
Out[55]: 
          a         b         c
0  0.806811  0.187630  0.978159
1  0.738792  0.862661  0.580592
2  0.224633  0.342579  0.214512
3  0.875262  0.151867  0.071244

In [56]: df.iloc[:, 0:3] ### Selective index based column ranges slicing
Out[56]: 
          a         b         c
0  0.806811  0.187630  0.978159
1  0.738792  0.862661  0.580592
2  0.224633  0.342579  0.214512
3  0.875262  0.151867  0.071244
Surya
  • 11,002
  • 4
  • 57
  • 39
9

You can access multiple columns by passing a list of column indices to dataFrame.ix.

For example:

>>> df = pandas.DataFrame({
             'a': np.random.rand(5),
             'b': np.random.rand(5),
             'c': np.random.rand(5),
             'd': np.random.rand(5)
         })

>>> df
          a         b         c         d
0  0.705718  0.414073  0.007040  0.889579
1  0.198005  0.520747  0.827818  0.366271
2  0.974552  0.667484  0.056246  0.524306
3  0.512126  0.775926  0.837896  0.955200
4  0.793203  0.686405  0.401596  0.544421

>>> df.ix[:,[1,3]]
          b         d
0  0.414073  0.889579
1  0.520747  0.366271
2  0.667484  0.524306
3  0.775926  0.955200
4  0.686405  0.544421
Safwan
  • 3,300
  • 1
  • 28
  • 33
3

The method .transpose() converts columns to rows and rows to column, hence you could even write

df.transpose().ix[3]
Stefano Fedele
  • 6,877
  • 9
  • 29
  • 48
2

Most of the people have answered how to take columns starting from an index. But there might be some scenarios where you need to pick columns from in-between or specific index, where you can use the below solution.

Say that you have columns A,B and C. If you need to select only column A and C you can use the below code.

df = df.iloc[:, [0,2]]

where 0,2 specifies that you need to select only 1st and 3rd column.

Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86
0

You can use the method take. For example, to select first and last columns:

df.take([0, -1], axis=1)
Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73