Python 3.x 需要从Dataframe到sql表的帮助:

Python 3.x 需要从Dataframe到sql表的帮助:,python-3.x,Python 3.x,如何使用Dataframe列在sql server中创建表?? 例如: import pandas as pd df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3'],'empid' : ['NA', 'NA', 'NA']}) df >>> df name empid 0 User 1 NA 1 User 2 NA 2 User 3 NA table name would be "

如何使用Dataframe列在sql server中创建表?? 例如:

import pandas as pd
df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3'],'empid' : ['NA', 'NA', 'NA']})
df

>>> df
     name  empid
0  User 1   NA
1  User 2   NA
2  User 3   NA

table name would be "users"

db_cursor.execute((CREATE TABLE new_table_name
(
 column names would be **as per in dataframe like name and empid**
))

我认为
sqlalchemy
到\u sql的
是您正在寻找的东西

from sqlalchemy import create_engine

# create an engine with databse connection string. for more refer to https://docs.sqlalchemy.org/en/13/core/engines.html
engine = create_engine('database_connection_string', echo=False)
# use the to_sql function to insert the df into 'new_table_name' table
df.to_sql('new_table_name', con=engine)