cam*_*enl 3 python indexing dataframe pandas
我不知道为什么熊猫在此数据帧上超出范围:
SC7a 2009 2010 2011 2012 2013 2014
Region 10 10.1 10.6 11.1 11.6 9.7 10.8
Georgia 7.5 7.4 7.8 7.6 7.2 7.1
Run Code Online (Sandbox Code Playgroud)
我正在做的是打电话给:
df.ix[:, 2014]
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:
IndexError: index 2014 is out of bounds for axis 0 with size 6
Run Code Online (Sandbox Code Playgroud)
请注意,调用loc可以正常工作:
df.loc[:, 2014]
SC7a
Region 10 10.8
Georgia 7.1
Name: 2014, dtype: float64
Run Code Online (Sandbox Code Playgroud)
这是错误吗?df.loc和df.ix不能与此数据互换吗?
我猜您df.columns.dtype是对象数据类型,并且测试df.columns.is_integer()返回false。
的行为ix取决于索引的数据类型或内容。如果您有整数类型的索引或.is_integer()为true,请ix使用基于标签的索引(例如loc)。如果您还有其他类型的索引(例如float,object),则ix在给定整数以查找时(例如iloc)使用基于位置的索引。在后一种情况下,索引位置2014超出范围。
如果将列转换为整数类型,ix则将按预期工作(它将仅使用基于标签的索引):
>>> df.columns = df.columns.astype(int)
>>> df.ix[:, 2014]
Region 10 10.8
Georgia 7.1
Name: 2014, dtype: float64
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6534 次 |
| 最近记录: |