pandas.to_dict 返回 None 与 nan 混合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43254699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas.to_dict returns None mixed with nan
提问by Piotr Kamoda
I've stumbled upon a minor problem with pandas and it's method to_dict. I have a table that I'm certain have equal number of identical columns in each row, let's say it looks like that:
我偶然发现了Pandas的一个小问题,它是 to_dict 方法。我有一个表格,我确定每行中的相同列数相同,假设它看起来像这样:
+----|----|----+
|COL1|COL2|COL3|
+----|----|----+
|VAL1| |VAL3|
| |VAL2|VAL3|
|VAL1|VAL2| |
+----|----|----+
When I do df.to_dict(orient='records')
I get:
当我这样做时df.to_dict(orient='records')
:
[{
"COL1":"VAL1"
,"COL2":nan
,"COL3":"VAL3"
}
,{
"COL1":None
,"COL2":"VAL2"
,"COL3":"VAL3"
}
,{
"COL1":"VAL1"
,"COL2":"VAL2"
,"COL3":nan
}]
Notice nan
's in some columns and None
's in other (always the same, there appears to be no nan
and None
in same column)
注意nan
's 在某些列中,而None
's 在其他列中(始终相同,似乎没有nan
和None
在同一列中)
And when I do json.loads(df.to_json(orient='records'))
i get only None
and no nan
's (which is desired output).
当我这样做时,我json.loads(df.to_json(orient='records'))
只得到None
而没有nan
's(这是所需的输出)。
Like this:
像这样:
[{
"COL1":"VAL1"
,"COL2":None
,"COL3":"VAL3"
}
,{
"COL1":None
,"COL2":"VAL2"
,"COL3":"VAL3"
}
,{
"COL1":"VAL1"
,"COL2":"VAL2"
,"COL3":None
}]
I would appreciate some explanation as to why it happens and if it can be controlled in some way.
我希望能解释一下为什么会发生这种情况以及是否可以以某种方式控制它。
==EDIT==
==编辑==
According to comments it would be better to first replace those nan
's with None
's, but those nan
's are not np.nan
:
根据评论,最好先用nan
's替换那些None
's,但那些nan
不是np.nan
:
>>> a = df.head().ix[0,60]
>>> a
nan
>>> type(a)
<class 'numpy.float64'>
>>> a is np.nan
False
>>> a == np.nan
False
回答by jezrael
I think you can only replace
, it is not possible control in to_dict
:
L = [{
"COL1":"VAL1"
,"COL2":np.nan
,"COL3":"VAL3"
}
,{
"COL1":None
,"COL2":"VAL2"
,"COL3":"VAL3"
}
,{
"COL1":"VAL1"
,"COL2":"VAL2"
,"COL3":np.nan
}]
df = pd.DataFrame(L).replace({np.nan:None})
print (df)
COL1 COL2 COL3
0 VAL1 None VAL3
1 None VAL2 VAL3
2 VAL1 VAL2 None
print (df.to_dict(orient='records'))
[{'COL3': 'VAL3', 'COL2': None, 'COL1': 'VAL1'},
{'COL3': 'VAL3', 'COL2': 'VAL2', 'COL1': None},
{'COL3': None, 'COL2': 'VAL2', 'COL1': 'VAL1'}]