为什么DataFrame.to_dict会在调用时返回不同的值?

时间:2017-01-10 00:37:25

标签: python pandas

我有一个DataFrame似乎行为异常(?)的方式如下:

>>> a=z.to_dict(orient='records')
>>> b=z.to_dict(orient='records')
>>> a1=json.dumps(a)
>>> b1=json.dumps(b)
>>> a1 == b1
True
>>> a == b
False # <<<===== WHAT?!!!
>>> for i in xrange(z.shape[0]):
      print i, a[i] == b[i]
0 True
...
9 True
10 True
11 True
12 False
13 True
14 True
...
20 True
21 False
22 True
...
29 False
30 True
...
40 True
41 True
42 False
43 True
44 True
...
50 False
51 True
52 False
...

这是什么意思?!

1 个答案:

答案 0 :(得分:1)

@ juanpa.arrivlllaga钉了它。在浮动数据框中,当单元格为np.nan np.nan == np.nan时,评估为False

z = pd.DataFrame(np.random.randn(10, 10), columns=list('abcdefghij'))
z

enter image description here

a=z.to_dict(orient='records')
b=z.to_dict(orient='records')
a1=json.dumps(a)
b1=json.dumps(b)

a == b

True

a1 == b1

True
z.loc[5, 'd'] = np.nan
z

enter image description here

a=z.to_dict(orient='records')
b=z.to_dict(orient='records')
a1=json.dumps(a)
b1=json.dumps(b)

a == b

True

a1 == b1

False