【发布时间】:2020-09-03 21:27:23
【问题描述】:
我有 2 个来自 dash 数据表的数据框,第一个是来自数据库的原始数据帧,第二个是来自已编辑数据表的数据帧。
在这种情况下,我有一列 product_date_up 表示上次编辑的时间戳。
我已经生成了每次用户单击保存按钮时添加时间戳的函数,并且时间戳会自动插入到已编辑的行中。
为此,我正在使用此代码
import sqlalchemy as alch
from datetime import datetime
@app.callback(
Output('table-container-edit','a'),
[Input('edit-button','n_clicks')],
[State('data-table-edit','data'),State('edit-button','click')]
)
def edit_db(n_clicks, dataset, click):
df_table = pd.DataFrame(dataset)
df_original = pd.read_sql(query2,engine)
for row in df_table['product_id']:
if df_table.loc[df_table['product_id']==row, 'product_name'] != df_original.loc[df_original['product_id']==row, 'product_name']:
df_table.loc[df_table['product_id']==row, 'product_date_up'] = datetime.now()
#if name from the edited table is different from database, do insert the timestamp
table_name = 'producttype'
df_table.to_sql(table_name,con=engine, index=False, if_exists='replace',
dtype={
'product_id': alch.CHAR(36),
'product_name': alch.VARCHAR(100),
'product_type': alch.VARCHAR(50),
'product_description': alch.VARCHAR(300),
'product_date_cr': alch.DateTime(),
'product_date_up': alch.DateTime(),
'product_by_cr': alch.VARCHAR(50),
'product_by_up': alch.VARCHAR(50)
})
似乎我无法将 pandas.Series 与 != 进行比较并收到此错误
Traceback (most recent call last):
File "D:\Software\Python 3.6.8\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "D:\Software\Python 3.6.8\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "D:\Software\Python 3.6.8\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "D:\Software\Python 3.6.8\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "D:\Software\Python 3.6.8\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "D:\Software\Python 3.6.8\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\Software\Python 3.6.8\lib\site-packages\dash\dash.py", line 1031, in dispatch
response.set_data(func(*args, outputs_list=outputs_list))
File "D:\Software\Python 3.6.8\lib\site-packages\dash\dash.py", line 966, in add_context
output_value = func(*args, **kwargs) # %% callback invoked %%
File "c:\Users\Kenny\Desktop\producttype.py", line 171, in edit_db
if df_table.loc[df_table['product_id']==row, 'product_name'] != df_original.loc[df_original['product_id']==row, 'product_name']:
File "D:\Software\Python 3.6.8\lib\site-packages\pandas\core\generic.py", line 1555, in __nonzero__
self.__class__.__name__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
我尝试过is not,但它正在更新整行。我可以使用什么?
编辑
根据 Gingerhaze 的回答和少量编辑,这段代码就像魔法一样工作
df_table.loc[(df_original["product_name"] != df_table["product_name"]),"product_date_up"]=datetime.now()
【问题讨论】:
-
请提供完整的回溯,而不仅仅是错误。
-
嗨我已经编辑了它@It_is_Chris
标签: python pandas dataframe datatable