Hello,
Is there a way how to insert pandas DataFrame into a database?
Assuming that I have a class defined with exactly same attributes as the DataFrame's columns are (same data type and column name).
I have been googling and didnt find an answer for that (I found answer on how to select data from database into DataFrame, which is also useful: https://stackoverflow.com/questions/42596206/how-to-convert-a-select-query-into-a-pandas-dataframe-using-peewee).
I can think of iterating over the rows of DataFrame and bulk-insert them.
Can you convert the elements of a DataFrame into dictionaries or tuples? I'm not too familiar with Pandas.
It supports conversion to dictionary: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_dict.html
Which basically returns dictionary of dictionaries or a list of dictionaries.
Dataframe is basically representing multiple rows to be inserted.
Can that dictionary be inserted as a "bulk" insert?
I found your post: https://stackoverflow.com/questions/22750439/peewee-how-to-convert-a-dict-into-a-model/22753644
In that post there is a dictionary representing one row, where the DataFrame contains many rows.
Right, so you would do:
# Convert a DF into a list of dicts.
list_of_dicts = convert_df_to_list_of_dicts(df)
MyModel.insert_many(list_of_dicts).execute()
Perfect, thank you!
Just to explicitly state the function it would be:
MyModel.insert_many(df.to_dict(orient='records')).execute()
Thanks for this excellent ORM!
Most helpful comment
Just to explicitly state the function it would be:
MyModel.insert_many(df.to_dict(orient='records')).execute()Thanks for this excellent ORM!