Python 如何在 Pandas 中按数字获取列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17193850/
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-19 00:43:52  来源:igfitidea点击:

How to get column by number in Pandas?

pythonpandas

提问by Jomme

What's the difference between:

有什么区别:

Maand['P_Sanyo_Gesloten']
Out[119]: 
Time
2012-08-01 00:00:11    0
2012-08-01 00:05:10    0
2012-08-01 00:10:11    0
2012-08-01 00:20:10    0
2012-08-01 00:25:10    0
2012-08-01 00:30:09    0
2012-08-01 00:40:10    0
2012-08-01 00:50:09    0
2012-08-01 01:05:10    0
2012-08-01 01:10:10    0
2012-08-01 01:15:10    0
2012-08-01 01:25:10    0
2012-08-01 01:30:10    0
2012-08-01 01:35:09    0
2012-08-01 01:40:10    0
...
2012-08-30 22:35:09    0
2012-08-30 22:45:10    0
2012-08-30 22:50:09    0
2012-08-30 22:55:10    0
2012-08-30 23:00:09    0
2012-08-30 23:05:10    0
2012-08-30 23:10:09    0
2012-08-30 23:15:10    0
2012-08-30 23:20:09    0
2012-08-30 23:25:10    0
2012-08-30 23:35:09    0
2012-08-30 23:40:10    0
2012-08-30 23:45:09    0
2012-08-30 23:50:10    0
2012-08-30 23:55:11    0
Name: P_Sanyo_Gesloten, Length: 7413, dtype: int64

And

Maand[[1]]
Out[120]: 
&ltclass 'pandas.core.frame.DataFrame'&gt
DatetimeIndex: 7413 entries, 2012-08-01 00:00:11 to 2012-08-30 23:55:11
Data columns (total 1 columns):
P_Sanyo_Gesloten    7413  non-null values
dtypes: int64(1)

How can I get data by column-indexnumber? And not by an Index-string?

如何通过列索引号获取数据?而不是通过索引字符串?

采纳答案by Andy Hayden

One is a column (aka Series), while the other is a DataFrame:

一个是列(又名系列),而另一个是 DataFrame:

In [1]: df = pd.DataFrame([[1,2], [3,4]], columns=['a', 'b'])

In [2]: df
Out[2]:
   a  b
0  1  2
1  3  4

The column 'b' (aka Series):

'b' 列(又名系列):

In [3]: df['b']
Out[3]:
0    2
1    4
Name: b, dtype: int64

The subdataframe with columns (position) in [1]:

[1] 中带有列(位置)的子数据框:

In [4]: df[[1]]
Out[4]:
   b
0  2
1  4

Note: it's preferable (and less ambiguous) to specify whether you're talking about the column name e.g. ['b'] or the integer location, since sometimes you can have columns named as integers:

注意:最好(并且不那么模糊)指定您是在谈论列名,例如 ['b'] 还是整数位置,因为有时您可以将列命名为整数:

In [5]: df.iloc[:, [1]]
Out[5]:
   b
0  2
1  4

In [6]: df.loc[:, ['b']]
Out[6]:
   b
0  2
1  4

In [7]: df.loc[:, 'b']
Out[7]:
0    2
1    4
Name: b, dtype: int64

回答by pandita

The following is taken from http://pandas.pydata.org/pandas-docs/dev/indexing.html. There are a few more examples... you have to scroll down a little

以下内容来自http://pandas.pydata.org/pandas-docs/dev/indexing.html。还有几个例子......你必须向下滚动一点

In [816]: df1

           0         2         4         6
0   0.569605  0.875906 -2.211372  0.974466
2  -2.006747 -0.410001 -0.078638  0.545952
4  -1.219217 -1.226825  0.769804 -1.281247
6  -0.727707 -0.121306 -0.097883  0.695775
8   0.341734  0.959726 -1.110336 -0.619976
10  0.149748 -0.732339  0.687738  0.176444

Select via integer slicing

通过整数切片选择

In [817]: df1.iloc[:3]

          0         2         4         6
0  0.569605  0.875906 -2.211372  0.974466
2 -2.006747 -0.410001 -0.078638  0.545952
4 -1.219217 -1.226825  0.769804 -1.281247

In [818]: df1.iloc[1:5,2:4]

          4         6
2 -0.078638  0.545952
4  0.769804 -1.281247
6 -0.097883  0.695775
8 -1.110336 -0.619976

Select via integer list

通过整数列表选择

In [819]: df1.iloc[[1,3,5],[1,3]]

           2         6
2  -0.410001  0.545952
6  -0.121306  0.695775
10 -0.732339  0.176444

回答by Wesam

Another way is to select a column with the columnsarray:

另一种方法是用columns数组选择一列:

In [5]: df = pd.DataFrame([[1,2], [3,4]], columns=['a', 'b'])

In [6]: df
Out[6]: 
   a  b
0  1  2
1  3  4

In [7]: df[df.columns[0]]
Out[7]: 
0    1
1    3
Name: a, dtype: int64