***import pandas as pd
employees = {'Name of Machining': ['milling','Drilling','Drilling','Chamfering'],
'Speed': [275,275,275,275],
'Feed': [0.28,0.03,0.03,0.28],
'Tool': ['EndMill','TwistDrill','TwistDrill','EndMill']
}
df = pd.DataFrame(employees, columns= ['Name of Machining','Speed','Feed','Tool'])***
i have used panda to contruct a table here. I am working on the project to save the table in
一个数据库我怎么能把这样构建的表保存在数据库中(
打印(df(
如果是MySQL数据库,则可以使用SQLAlchemy
工具包
请确保在您的终端中运行此程序(适用于Windows(以安装软件包:
pip install sqlalchemy pymysql
然后可以使用pandas.DataFrame.to_sql
创建、附加或覆盖表:
import pandas as pd
from sqlalchemy import create_engine
# - Creating the dataframe
employees = {'Name of Machining': ['milling','Drilling','Drilling','Chamfering'],
'Speed': [275,275,275,275],
'Feed': [0.28,0.03,0.03,0.28],
'Tool': ['EndMill','TwistDrill','TwistDrill','EndMill']
}
df = pd.DataFrame(employees)
# - Creating an engine to connect to MySQL Database
host, db, user, pw = "localhost", "mydb_name", "my_user_name", "my_password"
engine = create_engine(f"mysql+pymysql://{user}:{pw}@{host}/{db}")
# - Inseting the dataframe to the MySQL table
df.to_sql('employees', engine, index=False)
阅读官方文档中的此处以了解更多信息。