其他分享
首页 > 其他分享> > Dataframe转换json时删除空值

Dataframe转换json时删除空值

作者:互联网

问题:

使用df.to_json(orcient='records')时得到这样的结果:

[{"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}]'

解决办法:

import pandas as pd
import json

json.dumps([row.dropna().to_dict() for index,row in df.iterrows()])

 

标签:df,Dataframe,空值,json,dropna,import,null,row
来源: https://www.cnblogs.com/robin2022/p/16359057.html