如何将值存储到mysql数据库中,该数据库是一个由每一行中的列表组成的数据框。 以下是数据框:
0 [Nissan Motor, Carlos Ghosn]
1 [Nissan Motor, Carlos Ghosn]
2 []
3 [David Muir, Trio]
4 []
5 []
6 []
答案 0 :(得分:0)
像这样吗?
from sqlalchemy.dialects import postgresql
x = [["Nissan Motor", "Carlos Ghosn"], ["Nissan Motor", "Carlos Ghosn"], [], ["David Muir", "Trio"], [], [],[]]
df = pd.DataFrame({"data": x})
df.to_sql(con = "connection_string", name = "table_name", schema = "schema_name", if_exists="replace", dtype={'data': postgresql.JSONB}, index = False)
回读:
q = "select * from schema.table_name"
df = pd.read_sql(q, con="connection_string")
print(df)
输出:
data
0 [Nissan Motor, Carlos Ghosn]
1 [Nissan Motor, Carlos Ghosn]
2 []
3 [David Muir, Trio]
4 []
5 []
6 []
使用了JSONB