使用 .loc 和 bool 值的多维键出现 Pandas 错误

标签 pandas conditional-statements valueerror

尽管代码之前可以工作,但两周以来一直遇到同样的错误。不确定我是否将 pandas 更新为另一个库安装的一部分,也许那里发生了一些变化。目前版本为 23.4。预期结果是仅返回具有该标识符值的行。

In [42]: df.head()

Out[43]:

index   Identifier  ... 
0        51384710   ... 
1        74838J10   ... 
2        80589M10   ...     
3        67104410   ... 
4        50241310   ... 

[5 rows x 14 columns]
In [43]: df.loc[df.Identifier.isin(['51384710'])].head()
    Traceback (most recent call last):
      File "C:\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-44-a3dbf43451ef>", line 1, in <module>
     df.loc[df.Identifier.isin(['51384710'])].head()
File "C:\anaconda3\lib\site-packages\pandas\core\indexing.py", line 1478, in __getitem__
        return self._getitem_axis(maybe_callable, axis=axis)
File "C:\anaconda3\lib\site-packages\pandas\core\indexing.py", line 1899, in _getitem_axis
        raise ValueError('Cannot index with multidimensional key')
    **ValueError: Cannot index with multidimensional key**

Code Snippet

最佳答案

已修复。我做了 df.columns = [column_list] where column_list = [...],这导致 df 被视为具有多重索引,即使只有一个级别。从 df.columns 赋值中删除了括号。

关于使用 .loc 和 bool 值的多维键出现 Pandas 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53398022/

相关文章:

python - Pandas 将多个 csv 值分配给单个数据框列中的列表

powershell - 尝试将 powershell 脚本转换为 VBscript

dictionary - 条件 jsonpath 表达式在 groovy 脚本、jmeter 中不起作用

machine-learning - 为什么我收到 ValueError?

python-3.x - 传递值的长度为 1,索引意味着 260

tensorflow - TensorFlow 中的值错误

python - 如何在没有 pandas 中的 to_datetime 函数的情况下格式化列中的日期时间值?

pandas - 当 CSV 输入包含多个重复列时处理 pandas 中的查询?

python - pandas: KeyError 在一种情况下,但在另一种类似情况下则不然

javascript - 访问 Javascript 中未定义的值