Pandas TypeError: sequence item 0: expected str instance, dict found when using to_sql

Pandas TypeError: sequence item 0: expected str instance, dict found when using to_sql

我正在尝试将数据框导出到 mysql 数据库中。我通过订单和库存 API 调用获取数据。

我已经成功地将订单和库存 API 调用保存到数据帧中,并将订单数据帧导出到 MySQL table。

然而,Inventory 数据框向我抛出错误:

TypeError: sequence item 0: expected str instance, dict found

我不确定我做错了什么,我怀疑库存数据框在许多列中包含大量嵌套 json,但不确定该怎么做。

到目前为止,这是我的库存代码:

import pandas as pd

#python libary to compare today date for birthday lists.
import numpy as np
import datetime as dt
import datetime
from pandas.io.json import json_normalize
from pandas.io import sql
import pymysql.cursors
import json
import pymysql
import pandas.io.sql
from sqlalchemy import create_engine

headers_inventory = {
    'Accept': '',
    'Content-Type': '',
    'x-api-key': '',
    'x-organization-id': '',
    'x-facility-id': '',
    'x-user-id': '',
}

r_inventory = requests.get(' URL', headers=headers_inventory, verify=False)

data = json.loads(r_inventory.text)
df_inventory = json_normalize(data)
print (df_inventory)

engine = create_engine('mysql+pymysql://USERNAME:PWD@HOST:3306/DB')
df_inventory.to_sql("inventory", engine, if_exists="replace", index = False)

以下是数据框数据类型:

int64 
object
float64

将整个 df 转换为字符串有助于使用此行:

df = df.applymap(str)

有一个类似的问题 运行 一个财政年度的支点 table。问题如上,Python3好像没有把数字识别为一个类别。您必须将该系列转换为字符串(即将 2007、2008 更改为 FY2017、FY2018)。或者只需将该列更改为字符串,而不是整个 df ( df.column.astype(str) ).