gpt4 book ai didi

python - Pandas to_dict 使用 outtype ='records' 更改索引类型

转载 作者:太空狗 更新时间:2023-10-29 21:59:25 25 4
gpt4 key购买 nike

我正在尝试在以下 DataFrame 上调用 to_dict 函数:

将 pandas 导入为 pd

data = {"a": [1,2,3,4,5], "b": [90,80,40,60,30]}

df = pd.DataFrame(数据)

   a   b
0 1 90
1 2 80
2 3 40
3 4 60
4 5 30

df.reset_index().to_dict("r")

[{'a': 1, 'b': 90, 'index': 0},
{'a': 2, 'b': 80, 'index': 1},
{'a': 3, 'b': 40, 'index': 2},
{'a': 4, 'b': 60, 'index': 3},
{'a': 5, 'b': 30, 'index': 4}]

但是,如果我对数据帧执行浮点运算,就会出现问题,这会将索引变为 float :

(df*1.0).reset_index().to_dict("r")

[{'a': 1.0, 'b': 90.0, 'index': 0.0},  
{'a': 2.0, 'b': 80.0, 'index': 1.0},
{'a': 3.0, 'b': 40.0, 'index': 2.0},
{'a': 4.0, 'b': 60.0, 'index': 3.0},
{'a': 5.0, 'b': 30.0, 'index': 4.0}]

任何人都可以解释上述行为或推荐解决方法,或验证这是否可能是 pandas 错误?如上所示,to_dict 方法中的其他外类型都不会改变索引。

我已经在 pandas 0.14 和 0.18(最新)上复制了这个

非常感谢!

最佳答案

这个问题已经在github上回答了here

我将在此处传达答案,以便将问题标记为已解决并从未回答的 pandas 问题列表中移出。

来自 Github:

Nothing to do with the index, just the fact that you have any float dtypes in the data

If you look at the code, we use DataFrame.values, which returns a NumPy array, which must have a single dtype (float64 in this case).

-- TomAugspurger

该问题的解决方法是:

[x._asdict() for x in df.itertuples()]

生成 OrderedDict 对象列表

[OrderedDict([('Index', 0), ('a', 1.0), ('b', 90)]),
OrderedDict([('Index', 1), ('a', 2.0), ('b', 80)]),
OrderedDict([('Index', 2), ('a', 3.0), ('b', 40)]),
OrderedDict([('Index', 3), ('a', 4.0), ('b', 60)]),
OrderedDict([('Index', 4), ('a', 5.0), ('b', 30)])]

关于python - Pandas to_dict 使用 outtype ='records' 更改索引类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36548151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com