python pandas: export structure only (no rows) of a dataframe to SQL

一曲冷凌霜 提交于 2020-12-06 04:33:32

问题


I am using pandas 0.16 and sqlalchemy. Is it possible to export just the structure, i.e. column names and data types but no rows, of a dataframe to SQL?

The closest I managed to get to was to export the first row only:

df.ix[[0],:].to_sql( tablename, myconnection )

And then I'd have to do a truncate table. However, there are inconsistencies between the to_csv and the to_sql methods: to_csv writes boolean fields as the strings 'TRUE' or 'FALSE' , whereas to_sql writes them as 0 or 1. This means that importing files creates with dataframe.to_csv is more complicated than it should be.

If I run

df.ix[[],:].to_sql( tablename, myconnection )

that doesn't work because all columns are exported as text.


回答1:


You can use the get_schema function:

from pandas.io.sql import get_schema

engine = ...
df = ..
get_schema(df, 'table_name', con=engine)

This will give you the schema that would otherwise be created in string form, which you could execute with engine.execute

Further, the reason to_sql writes your boolean data as 0 and 1's, is because SQL Server has no boolean data type (see eg Is there a Boolean data type in Microsoft SQL Server like there is in MySQL?)




回答2:


.to_sql() supports a dict= argument that lets you specify the column types as SQLAlchemy types.

df.ix[[], :].to_sql(tablename, myconnection, dtype={
    'column1': sqlalchemy.types.Float,
    'column2': sqlalchemy.types.BigInt,
    'column3': sqlalchemy.types.Date,
})

... will let you map the columns to their respective SQLAlchemy types.




回答3:


Maybe you can try drop method to drop all rows.

import pandas as pd
from sqlalchemy import create_engine

df = pd.read_csv("c:\\Users\\ym\\Desktop\\out.csv")
# just drop all rows
df = df.drop(df.index[ [x for x in range(0,len(df))]])
engine = create_engine('mysql://root:root@127.0.0.1:3306/test?charset=utf8', echo=False)
df.to_sql(name=table_name, con=engine, if_exists='replace', chunksize=1000 ,index=False)


来源:https://stackoverflow.com/questions/29749356/python-pandas-export-structure-only-no-rows-of-a-dataframe-to-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!