如何在 pandas 中按数字获取列? [英] How to get column by number in Pandas?
本文介绍了如何在 pandas 中按数字获取列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
之间有什么区别
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
还有
Maand[[1]]
Out[120]:
<class 'pandas.core.frame.DataFrame'>
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)
如何通过column-indexnumber获取数据? 而不是通过索引字符串?
How can I get data by column-indexnumber? And not by an Index-string?
推荐答案
一个是列(也称为Series),而另一个是DataFrame:
One is a column (aka Series), while the other is a 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
"b"列(又名系列"):
The column 'b' (aka Series):
In [3]: df['b']
Out[3]:
0 2
1 4
Name: b, dtype: int64
在[1]中具有列(位置)的子数据框:
The subdataframe with columns (position) in [1]:
In [4]: df[[1]]
Out[4]:
b
0 2
1 4
注意:最好(且不要太含糊)指定您是否在谈论列名,例如['b']或整数位置,因为有时您可以将列命名为整数:
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:
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
这篇关于如何在 pandas 中按数字获取列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文