天天看點

python int64_轉換數字.int64到pandas中的python int

我有一個excel檔案和一張表。它包含兩列num1和num2,它們都有整數值。我正在嘗試使用Sqlalchemy和pandas來提取這些資料并将其插入Mysql資料庫。在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()

這段代碼給了我一個AttributeError

^{pr2}$

是以,在将dataframe轉換成dictionary之前,我嘗試了很多函數,比如astype,to_numeric來将資料類型轉換成普通的python int,但它們根本不起作用。這個問題似乎隻在dataframe具有所有整數值時才持續存在。如果至少有一個字元串或日期類型的列,則程式正常工作。我怎麼解決這個問題?在