问题描述
我偶然发现了一个熊猫的小问题,它是to_dict的方法.我有一个表,可以肯定每行中有相同数量的相同列,让我们说它看起来像这样:
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:
+----|----|----+
|COL1|COL2|COL3|
+----|----|----+
|VAL1| |VAL3|
| |VAL2|VAL3|
|VAL1|VAL2| |
+----|----|----+
当我执行df.to_dict(orient='records')
时,我得到:
[{
"COL1":"VAL1"
,"COL2":nan
,"COL3":"VAL3"
}
,{
"COL1":None
,"COL2":"VAL2"
,"COL3":"VAL3"
}
,{
"COL1":"VAL1"
,"COL2":"VAL2"
,"COL3":nan
}]
在某些列中注意nan
,在其他列中注意None
(总是一样,同一列中似乎没有nan
和None
)
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)
当我执行json.loads(df.to_json(orient='records'))
时,我只会得到None
而没有nan
(需要输出).
And when I do json.loads(df.to_json(orient='records'))
i get only None
and no nan
's (which is desired output).
赞:
[{
"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 ==
==EDIT==
根据评论,最好先用None
替换那些nan
,但是那些nan
不是np.nan
:
According to comments it would be better to first replace those nan
's with None
's, but those nan
's are not np.nan
:
>>> a = df.head().ix[0,60]
>>> a
nan
>>> type(a)
<class 'numpy.float64'>
>>> a is np.nan
False
>>> a == np.nan
False
推荐答案
我认为您只能 replace
,则无法在 to_dict
:
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'}]
这篇关于pandas.to_dict返回None与nan混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!