I have an excel file with one sheet. This contains two columns num1, num2 and both of them has integer values. I'm trying to pull this data and insert it into Mysql database using Sqlalchemy and pandas.
from sqlalchemy import create_engine, MetaData,Column,Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,validates
import pandas as pd
Base = declarative_base()
connection_string = # give your connection string here
engine= create_engine(connection_string)
Base.metadata.bind = engine
s = sessionmaker()
session = s()
class a(Base):
__tablename__ = 'a'
id = Column(Integer,primary_key=True)
num1 = Column(Integer)
num2 = Column(Integer)
a.__table__.create(checkfirst=True)
excel_sheet_path = # give path to the excel sheet
sheetname = # give your sheet name here
df = pd.read_excel(excel_sheet_path,sheetname).transpose()
dict = df.to_dict()
for i in dict.values():
session.add(a(**i))
session.commit()
This code throwing me an AttributeError saying
AttributeError: 'numpy.int64' object has no attribute 'translate'
So before converting dataframe into dictionary I tried many functions like astype, to_numeric to change the datatypes into normal python int, but they aren't working at all. The problem seems to persist only when the dataframe has all integer values. If you have atleast one another column with type string or date, Then the program works normally. How do I solve this ?