简体   繁体   English

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

[英]Pandas to_dict changes index type with outtype='records'

I'm am trying to call the to_dict function on the following DataFrame: 我正在尝试在以下DataFrame上调用to_dict函数:

import pandas as pd 将pandas导入为pd

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

df = pd.DataFrame(data) 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") 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}]

However my problem occurs if I perform a float operation on the dataframe, which mutates the index into a float: 但是,如果我对数据帧执行浮点运算,将索引变为浮点运算,则会出现问题:

(df*1.0).reset_index().to_dict("r") (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}]

Can anyone explain the above behaviour or recommend a workaround, or verify whether or not this could be a pandas bug? 任何人都可以解释上述行为或建议解决方法,或验证这是否可能是一个熊猫bug? None of the other outtypes in the to_dict method mutates the index as shown above. to_dict方法中的其他任何外键都没有改变索引,如上所示。

I've replicated this on both pandas 0.14 and 0.18 (latest) 我在大熊猫0.14和0.18(最新)上复制了这个

Many thanks! 非常感谢!

This question has been answered on github here 这个问题已在github上回答

I will convey the answer here so the question may be marked as solved and moved off the top-list of unanswered pandas questions. 我将在这里传达答案,以便将问题标记为已解决并移出无人接听的大熊猫问题的最高列表。

From Github: 来自Github:

Nothing to do with the index, just the fact that you have any float dtypes in the data 与索引无关,只是数据中有任何float dtypes这一事实

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). 如果查看代码 ,我们使用DataFrame.values,它返回一个NumPy数组,该数组必须有一个dtype(本例中为float64)。

-- TomAugspurger - TomAugspurger

A workaround for the problem would be: 该问题的解决方法是:

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

Which generates a list of OrderedDict objects 它生成一个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)])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM