Python 按位置选择熊猫列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14941097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:54:28  来源:igfitidea点击:

Selecting pandas column by location

pythonpandasindexing

提问by Jason Strimpel

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

我只是想通过一个整数访问命名的熊猫列。

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

您可以使用 来按位置选择一行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)})

采纳答案by DSM

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

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

回答by Adrian

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

您还可以使用df.icol(n)整数访问列。

Update: icolis deprecated and the same functionality can be achieved by:

更新:icol已弃用,可以通过以下方式实现相同的功能:

df.iloc[:, n]  # to access the column at the nth position

回答by Stefano Fedele

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

方法 .transpose() 将列转换为行,将行转换为列,因此您甚至可以编写

df.transpose().ix[3]

回答by Surya

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

您可以使用基于标签的 .loc 或基于索引的使用 .iloc 方法进行列切片,包括列范围:

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

回答by Safwan

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

您可以通过将列索引列表传递给 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