将python字典加载到JSON

时间:2018-07-18 11:27:37

标签: json python-3.x python-2.7 pandas

我们正在尝试将python字典转换为JSON文件。这样做时,我们在下面出现错误。

"TypeError: Object of type 'ndarray' is not JSON serializable"

这里是用于执行此操作的代码

1. Convert Pandas data frame to dictonary

   **records = df.to_dict(orient='records')**

2. replace NaN value with none

    **for record in records:
        print(record)
        for key in record:
            print(key)
            if record[key] != record[key]:
                record[key] = None**

3. then save dictionary to json file

   **out = '\n'.join([json.dumps(r, allow_nan=True) for r in records])**

在步骤3中,我们遇到了上面提到的错误。

谢谢

0 个答案:

没有答案