Pandas json_normalize在存在空值时将整数转换为浮点数

huangapple go评论107阅读模式
英文:

Pandas json_normalize converts ints to floats when there are null values

问题

当没有空值时,json_normalize可以将一些JSON数据展平成一个Pandas数据帧。但是,如果将{"test": 3}更改为{"test": None},那么整数数据对象将转换为float64类型:

import pandas as pd
from pandas.io.json import json_normalize

json = [{"test": 1}, {"test": 2}, {"test": None}]

df = json_normalize(json)

print(df)

结果为:

   test
0   1.0
1   2.0
2   NaN

是否有人找到了解决此问题的方法?

英文:

I am trying to use json_normalize to flatten some json into a pandas dataframe. When there are no null values, it works as expected:

import pandas as pd
from pandas.io.json import json_normalize

json = [{"test": 1}, {"test": 2}, {"test": 3}]

df = json_normalize(json)

print(df)

Returns:

   test
0     1
1     2
2     3

However, if I change {"test": 3} to {"test": None} then the following the int64 data objects are converted to float64 type:

   test
0   1.0
1   2.0
2   NaN

has anyone found a workaround for this issue?

答案1

得分: 2

这是因为整数不能是NaN类型。

一个解决方法可能是将所有内容都保持为字符串:

json = [{"test": "1"}, {"test": "2"}, {"test": "None"}]

然后你会得到

   test
0     1
1     2
2  None
英文:

This is because integers can't be of NaN type.

One workaround could be to keep everything as a string:

json = [{"test": "1"}, {"test": "2"}, {"test": "None"}]

and you'll get

   test
0     1
1     2
2  None

huangapple
  • 本文由 发表于 2020年1月6日 18:38:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定