繁体   English   中英

从熊猫数据框中批量更新peewee数据库的最有效方法

[英]Most efficient way to batch update a peewee database from a pandas dataframe

数据库看起来像这样:

date            value
2000-01-01      foo
2000-01-01      foo
2000-01-01      foo
2000-01-02      bar
2000-01-02      bar
2000-01-02      bar
2000-01-10      yyy
2000-01-10      yyy
2000-01-10      yyy

熊猫数据MyDataframe如下所示:

date            value
2000-01-01      new_foo
2000-01-02      new_bar
2000-01-10      new_yyy

您可能已经猜到了,我需要数据库看起来像这样:

date            value
2000-01-01      new_foo
2000-01-01      new_foo
2000-01-01      new_foo
2000-01-02      new_bar
ecc...

我可以遍历MyDataframe并运行一系列.update

for date, value in MyDataframe:
    query = MyModel.update(value=value).where(MyModel.date == date).execute()
    query.execute()

我的问题是:有一种方法可以只通过一次调用execute()execute() (或其他更有效的方法)? bulk_execute(array_of_queries)类的东西?

有什么方法可以将数据帧直接馈送到.update()吗? 像这样:

MyModel.update(value=MyDataframe.loc[MyModel.date]).execute()

不幸的是,这不起作用:传递给.loc[]的索引不是实际值,而是DateTimeField对象。 确实,它给出了这个错误:

KeyError('the label [<DateTimeField: MyModel.date>] is not in the [index]',)

文档建议您可以在更新功能中运行实际代码,并提供以下示例:

Employee.update(bonus=(Employee.bonus + (Employee.salary * .1)))

您可以尝试合并数据框并替换原始值列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM