JSON inside column DataFrame



我正在尝试进行批量插入在Postgres中,我的表有一个字段类型JSON我想在上面插入原始JSON,但是当我试图使它,python从双引号改为"到单引号',它在技术上破坏了DataFrame内的JSON列,我正在寻找一种方法来做这个大容量插入。

首先我得到我的数据json格式,接下来我做一个数据框架的数据操作和清理,最后我想在Postgres中插入这个DF。

df = pd.DataFrame(response['data'])

,这就是python如何从{ "age_max": 44, "age_min": [20,30] }:{ 'age_max': 44, 'age_min': [20,30] }

pandas自动将json转换为字典对象。您可以使用内置的json模块中的dumps轻松地将字典转换为json。

import requests
from json import dumps
import pandas
import psycopg2
#sample dataset 
df = pandas.DataFrame.from_dict(
{'date': {0: '2021-02-16',
1: '2021-02-15',
2: '2021-02-14',
3: '2021-02-13',
4: '2021-02-12'},
'name': {0: 'East Midlands',
1: 'East Midlands',
2: 'East Midlands',
3: 'East Midlands',
4: 'East Midlands'},
'cases': {0: {'new': 174, 'cumulative': 294582},
1: {'new': 1477, 'cumulative': 294408},
2: {'new': 899, 'cumulative': 292931},
3: {'new': 898, 'cumulative': 292032},
4: {'new': 1268, 'cumulative': 291134}}}
)
df['json'] = df['cases'].apply(dumps) #create new series running the function json.dumps against each element in the series
p = df[['date', 'name', 'json']].values.tolist() #create parameter list
con = db_connection() #replace with your db connection function or  psycopg2.connect()
csr = con.cursor()
sql = """insert into corona (date, name, json) values (%s, %s, %s)"""
csr.executemany(sql, params=p)
con.commit()
con.close()

最新更新