将pandas dataframe转换为元组列表并删除所有pandas数据类型

时间:2016-08-08 06:25:49

标签: python pandas numpy dataframe

我需要将数据帧转换为可插入sql表的格式。

data_tuples = [tuple(row) for row in df.values]

如何从pandas数据框中删除所有非python数据类型(包括np int和nans以及NaT)?

1 个答案:

答案 0 :(得分:3)

如果您想有效地使用它,请使用相应的pandas方法 - to_sql()

from sqlalchemy import create_engine

# conn = create_engine('postgresql://user:password@host:port/dbname')
conn = create_engine('postgresql+psycopg2://user:password@host:port/dbname')
df.to_sql('table_name', conn)

其中conn是SQLAlchemy引擎连接对象

Docs: using SQLAlchemy with PostgreSQL