I have a Python dictionary, but when I try to dump it as a JSON file, it says it is a Pandas DataFrame!
import pandas as pd
import os
import re
import json
files_net = [f for f in os.listdir('.') if re.match(r'network_.+.json', f)]
networks = {}
for file in files_net:
with open(file) as f:
networks[re.search(r'network_(.+).json', file).group(1)] = pd.read_json(f)
type(networks)
>>> dict
with open('networks.json', 'w') as f:
json.dump(networks, f)
TypeError: Object of type DataFrame is not JSON serializable
Yes networks
is a dictionary, but values of it are pd.DataFrame
s because they come from pd.read_json
; hence the error. You might try to_json
whilst forming the key-value pairs:
networks[re.search(r'network_(.+).json', file).group(1)] = pd.read_json(f).to_json()
But since you are read
ing json
anyway, you might use json
module in the first place eg json.load(f)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.