python - 可以使用 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()
最后一行是否以某种方式从表中删除了列/列名?
解决方案
ix
已被弃用。ix
一直模棱两可:是ix[10]
指标签为 10 的行,还是位置 10 的行?
使用loc
oriloc
代替:
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),它会返回一个错误,以及一个弃用警告,所以我猜开发人员将其更改为.d
3
loc
如果要使用混合索引,则必须明确说明:
s.loc[3] if 3 in s.index else s.iloc[3]
推荐阅读
- excel - 如何更改一列未格式化的数字,以便 Excel 可以使用 VBA 将它们实际读取为数字
- tsql - concat 使用空格作为分隔符标记问题而不是空格
- python - Firebird 检查列中的类型
- c - 字符串的大小
- ios - Swift Decode JSON - 无法解码
- javascript - NestJS 无法解决 JWT_MODULE_OPTIONS 的依赖关系(同样的问题,不同的解决方案)
- browser - IP 地址可以是有效的域名吗?
- c# - EntityFramework 与另一个代码库冲突 - 迁移失败
- git - 不小心使用了错误的 git-restore 命令 - 我的所有文件都消失了吗?
- javascript - 向 XMLHttpRequest 添加 onerror 函数