Peewee: Inserting pandas DataFrame

Created on 18 Jun 2018  路  5Comments  路  Source: coleifer/peewee

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.

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!

All 5 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikemill picture mikemill  路  3Comments

kadnan picture kadnan  路  3Comments

hanxifu picture hanxifu  路  4Comments

MozzieHan picture MozzieHan  路  4Comments

alexpantyukhin picture alexpantyukhin  路  5Comments