Pandas sql
Jump to navigation
Jump to search
Prep for sqlite3
import pandas as pd import sqlite3 def write_sql_from_dfs(): for table, df in dfs.items(): # loop through `dict` of dataframes uuids = [] for i in range(len(df.index)): uuids.append(uuid.uuid4()) df.insert(0, "uuid", uuids) df['timestamp'] = pd.Timestamp("now") # df['timestamp'] = [pd.Timestamp("now") for _ in range(len(df.index))] for i in df.columns: for i in df.columns: df[i] = df[i].astype("string") df.to_sql(name=table, con=cnx, if_exists='append', index=False) def main(): global cnx cnx = sqlite3.connect(f"mydb.sqlite3") write_sql_from_dfs() if __name__ == "__main__": main()