在 pandas 中使用元组作为索引键时,如何“通过传入类别参数显式指定类别顺序”?

2024-01-23

我一直在试图弄清楚如何使这些元组索引键pandas但我收到错误。

我如何使用错误中的建议pd.Categorical下面修复这个错误?

我知道我可以转换为字符串,但我很好奇错误消息中的建议是什么意思?

当我运行它时,效果非常好0.22.0。我已经开了一个GitHub问题 https://github.com/pandas-dev/pandas/issues/22832为此,如果有人想看到正确的输出0.22.0.

我想更新我的 pandas 并妥善处理这个问题。

Running this with the current pandas 0.23.4:
import sys; sys.version
# '3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33) \n[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]'
import pandas as pd; pd.__version__
# '0.23.4'
index = [(('criterion', 'gini'), ('max_features', 'log2'), ('min_samples_leaf', 1)), (('criterion', 'gini'), ('max_features', 'log2'), ('min_samples_leaf', 2)), (('criterion', 'gini'), ('max_features', 'log2'), ('min_samples_leaf', 3)), (('criterion', 'gini'), ('max_features', 'log2'), ('min_samples_leaf', 5)), (('criterion', 'gini'), ('max_features', 'log2'), ('min_samples_leaf', 8)), (('criterion', 'gini'), ('max_features', 'sqrt'), ('min_samples_leaf', 1)), (('criterion', 'gini'), ('max_features', 'sqrt'), ('min_samples_leaf', 2)), (('criterion', 'gini'), ('max_features', 'sqrt'), ('min_samples_leaf', 3)), (('criterion', 'gini'), ('max_features', 'sqrt'), ('min_samples_leaf', 5)), (('criterion', 'gini'), ('max_features', 'sqrt'), ('min_samples_leaf', 8)), (('criterion', 'gini'), ('max_features', None), ('min_samples_leaf', 1)), (('criterion', 'gini'), ('max_features', None), ('min_samples_leaf', 2)), (('criterion', 'gini'), ('max_features', None), ('min_samples_leaf', 3)), (('criterion', 'gini'), ('max_features', None), ('min_samples_leaf', 5)), (('criterion', 'gini'), ('max_features', None), ('min_samples_leaf', 8)), (('criterion', 'gini'), ('max_features', 0.382), ('min_samples_leaf', 1)), (('criterion', 'gini'), ('max_features', 0.382), ('min_samples_leaf', 2)), (('criterion', 'gini'), ('max_features', 0.382), ('min_samples_leaf', 3)), (('criterion', 'gini'), ('max_features', 0.382), ('min_samples_leaf', 5)), (('criterion', 'gini'), ('max_features', 0.382), ('min_samples_leaf', 8)), (('criterion', 'entropy'), ('max_features', 'log2'), ('min_samples_leaf', 1)), (('criterion', 'entropy'), ('max_features', 'log2'), ('min_samples_leaf', 2)), (('criterion', 'entropy'), ('max_features', 'log2'), ('min_samples_leaf', 3)), (('criterion', 'entropy'), ('max_features', 'log2'), ('min_samples_leaf', 5)), (('criterion', 'entropy'), ('max_features', 'log2'), ('min_samples_leaf', 8)), (('criterion', 'entropy'), ('max_features', 'sqrt'), ('min_samples_leaf', 1)), (('criterion', 'entropy'), ('max_features', 'sqrt'), ('min_samples_leaf', 2)), (('criterion', 'entropy'), ('max_features', 'sqrt'), ('min_samples_leaf', 3)), (('criterion', 'entropy'), ('max_features', 'sqrt'), ('min_samples_leaf', 5)), (('criterion', 'entropy'), ('max_features', 'sqrt'), ('min_samples_leaf', 8)), (('criterion', 'entropy'), ('max_features', None), ('min_samples_leaf', 1)), (('criterion', 'entropy'), ('max_features', None), ('min_samples_leaf', 2)), (('criterion', 'entropy'), ('max_features', None), ('min_samples_leaf', 3)), (('criterion', 'entropy'), ('max_features', None), ('min_samples_leaf', 5)), (('criterion', 'entropy'), ('max_features', None), ('min_samples_leaf', 8)), (('criterion', 'entropy'), ('max_features', 0.382), ('min_samples_leaf', 1)), (('criterion', 'entropy'), ('max_features', 0.382), ('min_samples_leaf', 2)), (('criterion', 'entropy'), ('max_features', 0.382), ('min_samples_leaf', 3)), (('criterion', 'entropy'), ('max_features', 0.382), ('min_samples_leaf', 5)), (('criterion', 'entropy'), ('max_features', 0.382), ('min_samples_leaf', 8))]
len(index)
# 40
pd.Index(index)
Traceback (most recent call last):
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/algorithms.py", line 635, in factorize
    order = uniques.argsort()
TypeError: '<' not supported between instances of 'NoneType' and 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/sorting.py", line 451, in safe_sort
    sorter = values.argsort()
TypeError: '<' not supported between instances of 'NoneType' and 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/arrays/categorical.py", line 345, in __init__
    codes, categories = factorize(values, sort=True)
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/util/_decorators.py", line 178, in wrapper
    return func(*args, **kwargs)
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/algorithms.py", line 643, in factorize
    assume_unique=True)
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/sorting.py", line 455, in safe_sort
    ordered = sort_mixed(values)
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/sorting.py", line 441, in sort_mixed
    nums = np.sort(values[~str_pos])
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 847, in sort
    a.sort(axis=axis, kind=kind, order=order)
TypeError: '<' not supported between instances of 'NoneType' and 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 449, in __new__
    data, names=name or kwargs.get('names'))
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/indexes/multi.py", line 1330, in from_tuples
    return MultiIndex.from_arrays(arrays, sortorder=sortorder, names=names)
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/indexes/multi.py", line 1274, in from_arrays
    labels, levels = _factorize_from_iterables(arrays)
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/arrays/categorical.py", line 2543, in _factorize_from_iterables
    return map(list, lzip(*[_factorize_from_iterable(it) for it in iterables]))
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/arrays/categorical.py", line 2543, in <listcomp>
    return map(list, lzip(*[_factorize_from_iterable(it) for it in iterables]))
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/arrays/categorical.py", line 2515, in _factorize_from_iterable
    cat = Categorical(values, ordered=True)
  File "/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pandas/core/arrays/categorical.py", line 351, in __init__
    raise TypeError("'values' is not ordered, please "
TypeError: 'values' is not ordered, please explicitly specify the categories order by passing in a categories argument

我能找到的最接近你想做的事情是:pd.DataFrame(index, dtype='category').set_index([0, 1, 2]).index

返回以下内容:

MultiIndex(levels=[[('criterion', 'entropy'), ('criterion', 'gini')], [('max_features', 'log2'), ('max_features', 'sqrt'), ('max_features', None), ('max_features', 0.382)], [('min_samples_leaf', 1), ('min_samples_leaf', 2), ('min_samples_leaf', 3), ('min_samples_leaf', 5), ('min_samples_leaf', 8)]],
       labels=[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3], [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]],
       names=[0, 1, 2])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 pandas 中使用元组作为索引键时,如何“通过传入类别参数显式指定类别顺序”? 的相关文章

随机推荐

  • 编译curl后路径问题

    我使用以下命令克隆存储库后编译了curl buildconf configure with libssh2 make sudo make install 但是 在 sudo make install 之后 如果 I run curl V 我
  • Mysql 中的行级锁定

    我的表中有 5 行 1 到 5 我想要第 2 行锁定以进行某些更新 同时如果有人尝试更新第 4 行 那么他应该能够更新 我正在使用下面的代码尝试此操作 但我觉得它将锁定放在表级别而不是行级别 第一节 START TRANSACTION SE
  • iOS 导航栏项目图像大小

    我想定制我的NavigationBar按钮并使用我自己的图像 问题是尺寸应该是多少 我发现按钮大小应该是40 40 所以视网膜图像应该是80 80 这些是尺寸文档 https developer apple com ios human in
  • 如何使用 Pig 从列中解析 JSON 字符串

    我有 tsv 日志文件 其中一列由 json 字符串填充 我想用以下内容解析该列JsonLoader in a Pig脚本 我看到很多例子JsonLoader用于每行只有一个 json 字符串的情况 我还有其他专栏想要跳过 但我不知道该怎么
  • 我们如何设置ContentRootPath和WebRootPath?

    当我们从 IIS 运行我们的应用程序时 我们最终得到以下 ContentRoot 和 WebRoot ContentRoot C MyApp wwwroot WebRoot C MyApp wwwroot wwwroot 这是我们的设置方式
  • CSS 未在 Heroku 中加载

    我将应用程序部署到 Heroku 但仅加载资产 图像 CSS 文件 尤其是包含 90 CSS 的 custom css 未加载 我在本地预编译了资产并将它们推送到 Heroku 但没有成功 仅加载图像 而不加载 custom css 文件
  • p:dataTable 中的 h:selectOneMenu 未提交其值

    我有关于 selectOneMenu 和设置值的问题 我有一张对象示例光盘 其中包含 ID 文本和List
  • 在 C# Windows 窗体中预览 .doc、.docx [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在用 C 开发一个 Windows 窗体项目 现在我可以使用 ToolBox 中的 WebBrow
  • 如何比较车把中的值?

    我想根据条件显示不同的 HTML 它似乎没有比较这两个值 并且总是显示第一个变体 如何将预定义值与 JSON 中的原始值进行比较 以便它可以正确执行 each this each visits div class row if variab
  • 带参数的 Jest Mock Promise

    这是我尝试在 Jest 中编写单元测试的方法 async function getParameter parameter string withDecryption boolean false Promise
  • Jquery,拖放并保存到mysql数据库?

    我在网上找了好久 只找到了inetuts with cookies 它教如何使用Jquery进行拖放 然后保存在cookie中 谁能告诉我如何保存到数据库 php 和 mysql 我非常需要它 EDIT 首先 我不是php初学者 而是AJA
  • 预验证类中的 java.lang.IllegalAccessError 类引用解析为意外实现

    当我按下启动 MainActivity 中应该启动新活动的方法的按钮时 出现 预验证类中的 java lang IllegalAccessError 类引用解析为意外实现 错误 我可以做什么来解决这个问题 这是我第一次尝试制作 Androi
  • Nativescript ios - 命令 xcodebuild 失败,退出代码为 null

    我通过运行以下命令设置了一个新的 nativescript 应用程序 tns create application ng 当我尝试使用以下方式启动应用程序时 tns run ios emulator 我收到以下错误 Unable to ap
  • 检测菜单项单击上的鼠标左键/右键吗?

    在Delphi XE2中 如何检测用户是否用鼠标左键或右键单击了弹出菜单项 使用该单元 将其作为组件安装并替换标准TPopupMenu这增加了一个OnMenuRightClick event unit RCPopupMenu interfa
  • 如何在使用 iframe 时停止 IE 11 上的自动下载

    我已经在 Iframe 标记中给出了源 我的问题是 当页面在 IE 上加载时 下载会自动开始 并且通常发生在 Windows 8 上安装的 IE 上 div div 下载可能是因为没有Adobe Reader插件 安装 在这种情况下 IE
  • 在 SoundCloud iOS 应用程序中打开曲目的 URL

    我想在 SoundCloud iOS 应用程序中打开 SoundCloud 曲目 我的印象是使用正确的 url 方案是soundcloud track track id 这将打开 SoundCloud 应用程序 但不会选择正确的曲目 任何人
  • 无法在 Python 3.10 上安装 Matplotlib

    python3 10 m pip install user matplotlib 当我运行上面的命令来安装 Matplotlib 时 我不断收到以下错误 我最初遇到 C 错误 然后我安装了 Visual Studio Time Elapse
  • 任何像 recyclerview 或 javafx 的可重用视图之类的东西

    我正在创建一个滚动窗格 它显示从 sqlite 数据库获取的数据集 这些数据以这样的方式显示 即它们排列在重复的 ui 集中 就像在 android 中的 recyclerview 中一样 有什么方法可以实现它 因为使用 javafx 定位
  • 如何阻止 Exchange 自动将纯文本电子邮件转换为 HTML?

    我已经为将由我的代码解析的电子邮件设置了一个 Exchange 2003 邮箱 电子邮件以纯文本形式发送 我的代码希望以纯文本形式接收它们 但是 Exchange 似乎会自动将它们转换为 HTML 我如何阻止它这样做并只按照发送的方式接收电
  • 在 pandas 中使用元组作为索引键时,如何“通过传入类别参数显式指定类别顺序”?

    我一直在试图弄清楚如何使这些元组索引键pandas但我收到错误 我如何使用错误中的建议pd Categorical下面修复这个错误 我知道我可以转换为字符串 但我很好奇错误消息中的建议是什么意思 当我运行它时 效果非常好0 22 0 我已经