【问题标题】:Can use dataframe ix for assignment, but not retrieval可以使用 dataframe ix 进行分配,但不能检索
【发布时间】:2019-12-05 03:58:03
【问题描述】:

我正在循环遍历 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()

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

【问题讨论】:

  • 是的。 Run 列已消失,该列的唯一值现在是您的索引。
  • 谢谢!之后有没有办法保留 Run 列?当我将数据打印到 csv 时仍然存在(我猜是索引,从 csv 中不清楚)。

标签: python pandas


【解决方案1】:

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

请改用lociloc

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

根据文档,s.ix[3] 会返回 d,因为它首先搜索标签 3。当失败时,它会回到位置 3。在我的机器(Pandas 0.24.2)上,它会返回一个错误以及一个弃用警告,所以我猜开发人员将其更改为类似于loc

如果您想使用混合索引,您必须明确说明:

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

【讨论】:

  • 谢谢,我不太确定如何正确翻译这些,我仍然想使用“混合”索引。但是,在您的陈述中,如果它们都按标签和位置返回值,那么按标签或按位置到底有什么区别?
  • 虽然这是很好的信息,但它并没有回答最初的问题,即列名是否在组中被删除。
  • 感谢您提供详细信息。但是,我返回我的代码并将对 df.ix[i,"col name"] 的每个调用替换为 df.iloc[i]["col name"] 并且结果是一个空白数据框,或者更确切地说是空白我最初初始化的数据框。为什么我不能简单地将 df.ix[i,"col name"] 替换为 df.iloc[i]["col name"]?
猜你喜欢
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 2020-08-06
相关资源
最近更新 更多