英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论