首页 > 解决方案 > 可以使用 dataframe ix 进行分配,但不能检索

问题描述

我正在循环遍历 pandas df 的行,循环索引 i。我可以使用 ix 函数分配多个列,其中循环索引作为第一个参数,列名作为第二个参数。但是,当我尝试使用此方法检索/打印时,

print(df.ix[i,"Run"])

我得到以下类型错误:str 对象不能被解释为整数。某种程度上与 Keyerror: 'Run' 相关

不太清楚为什么会发生这种情况,因为 Run 确实是数据框中的一列。有什么建议么?

Traceback (most recent call last):
  File \!"C:\WPy-3670\python-3.6.7.amd64\lib\site-packages\pandas\core\indexes\base.py\!", line 3124, in get_value
    return libindex.get_value_box(s, key)
  File \!"pandas\_libs\index.pyx\!", line 55, in pandas._libs.index.get_value_box
  File \!"pandas\_libs\index.pyx\!", line 63, in pandas._libs.index.get_value_box
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File \!"C:\...", line 365, in <module>
    print(df.ix[i,\!"Run\!"])
  File \!"C:\WPy-3670\python-3.6.7.amd64\lib\site-packages\pandas\core\indexing.py\!", line 116, in __getitem__
    return self._getitem_tuple(key)
  File \!"C:\WPy-3670\python-3.6.7.amd64\lib\site-packages\pandas\core\indexing.py\!", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File \!"C:\WPy-3670\python-3.6.7.amd64\lib\site-packages\pandas\core\indexing.py\!", line 1027, in _getitem_lowerdim
    return getattr(section, self.name)[new_key]
  File \!"C:\WPy-3670\python-3.6.7.amd64\lib\site-packages\pandas\core\indexing.py\!", line 122, in __getitem__
    return self._getitem_axis(key, axis=axis)
  File \!"C:\WPy-3670\python-3.6.7.amd64\lib\site-packages\pandas\core\indexing.py\!", line 1116, in _getitem_axis
    return self._get_label(key, axis=axis)
  File \!"C:\WPy-3670\python-3.6.7.amd64\lib\site-packages\pandas\core\indexing.py\!", line 136, in _get_label
    return self.obj[label]
  File \!"C:\WPy-3670\python-3.6.7.amd64\lib\site-packages\pandas\core\series.py\!", line 767, in __getitem__
    result = self.index.get_value(self, key)
  File \!"C:\WPy-3670\python-3.6.7.amd64\lib\site-packages\pandas\core\indexes\base.py\!", line 3132, in get_value
    raise e1
  File \!"C:\WPy-3670\python-3.6.7.amd64\lib\site-packages\pandas\core\indexes\base.py\!", line 3118, in get_value
    tz=getattr(series.dtype, 'tz', None))
  File \!"pandas\_libs\index.pyx\!", line 106, in pandas._libs.index.IndexEngine.get_value
  File \!"pandas\_libs\index.pyx\!", line 114, in pandas._libs.index.IndexEngine.get_value
  File \!"pandas\_libs\index.pyx\!", line 162, in pandas._libs.index.IndexEngine.get_loc
  File \!"pandas\_libs\hashtable_class_helper.pxi\!", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File \!"pandas\_libs\hashtable_class_helper.pxi\!", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Run'
"

将我打印的列名称更改为任何其他列后,它确实可以正常工作。在代码的前面,我使用以下内容“压缩”了行,这些行在“运行”列中每个唯一字符串都有多行。

df=df.groupby('Run').max()

最后一行是否以某种方式从表中删除了列/列名?

标签: pythonpandas

解决方案


ix已被弃用ix一直模棱两可:是ix[10]指标签为 10 的行,还是位置 10 的行?

使用locoriloc代替:

df.loc[i,"Run"] = ... # by label

df.iloc[i]["Run"] = ... # by position

至于groupby删除Run:它移动Run到数据框的索引。要将其作为列取回,请调用reset_index

df=df.groupby('Run').max().reset_index()

按标签和位置索引的区别:

假设你有一个这样的系列:

s = pd.Series(['a', 'b', 'c', 'd', 'e'], index=np.arange(0,9,2))

0    a
2    b
4    c
6    d
8    e

第一列是标签(又名索引)。第二列是系列的值。

基于标签的索引:

s.loc[2] --> b
s.loc[3] --> error. The label doesn't exist

基于位置的索引:

s.iloc[2] --> c. since `a` has position 0, `b` has position 1, and so on
s.iloc[3] --> d

根据文档,自从它第一次搜索 label时s.ix[3]就会返回。当失败时,它会回到位置 3。在我的机器上(Pandas 0.24.2),它会返回一个错误,以及一个弃用警告,所以我猜开发人员将其更改为.d3loc

如果要使用混合索引,则必须明确说明:

s.loc[3] if 3 in s.index else s.iloc[3]

推荐阅读