pandas 在to_json时删除空值 [英] Pandas remove null values when to_json
问题描述
我实际上有一个熊猫数据框,我想将其保存为json格式. 从熊猫文档中说:
i have actually a pandas dataframe and i want to save it to json format. From the pandas docs it says:
注意NaN,NaT和None将转换为null和日期时间 对象将基于date_format和date_unit进行转换 参数
Note NaN‘s, NaT‘s and None will be converted to null and datetime objects will be converted based on the date_format and date_unit parameters
然后使用东方选项records
我有类似的东西
Then using the orient option records
i have something like this
[{"A":1,"B":4,"C":7},{"A":null,"B":5,"C":null},{"A":3,"B":null,"C":null}]
是否可以代替它:
[{"A":1,"B":4,"C":7},{"B":5},{"A":3}]'
谢谢
推荐答案
以下内容接近您想要的内容,从本质上讲,我们创建了非NaN值的列表,然后对此调用了to_json
:
The following gets close to what you want, essentially we create a list of the non-NaN values and then call to_json
on this:
In [136]:
df.apply(lambda x: [x.dropna()], axis=1).to_json()
Out[136]:
'{"0":[{"a":1.0,"b":4.0,"c":7.0}],"1":[{"b":5.0}],"2":[{"a":3.0}]}'
在此处创建列表是必要的,否则它将尝试使结果与原始df形状对齐,这将重新引入您要避免的NaN
值:
creating a list is necessary here otherwise it will try to align the result with your original df shape and this will reintroduce the NaN
values which is what you want to avoid:
In [138]:
df.apply(lambda x: pd.Series(x.dropna()), axis=1).to_json()
Out[138]:
'{"a":{"0":1.0,"1":null,"2":3.0},"b":{"0":4.0,"1":5.0,"2":null},"c":{"0":7.0,"1":null,"2":null}}'
同时在dropna
的结果上调用list
会广播带有形状的结果,例如填充:
also calling list
on the result of dropna
will broadcast the result with the shape, like filling:
In [137]:
df.apply(lambda x: list(x.dropna()), axis=1).to_json()
Out[137]:
'{"a":{"0":1.0,"1":5.0,"2":3.0},"b":{"0":4.0,"1":5.0,"2":3.0},"c":{"0":7.0,"1":5.0,"2":3.0}}'
这篇关于 pandas 在to_json时删除空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!