获取错误ValueError:序列的真值不明确.使用a.empty、a.bool()、a.item()、.any()或.



我有两个来自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)
})

我似乎无法与熊猫相比。与!=系列并得到此错误

Traceback (most recent call last):
File "D:SoftwarePython 3.6.8libsite-packagesflaskapp.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "D:SoftwarePython 3.6.8libsite-packagesflaskapp.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "D:SoftwarePython 3.6.8libsite-packagesflaskapp.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "D:SoftwarePython 3.6.8libsite-packagesflask_compat.py", line 39, in reraise
raise value
File "D:SoftwarePython 3.6.8libsite-packagesflaskapp.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "D:SoftwarePython 3.6.8libsite-packagesflaskapp.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:SoftwarePython 3.6.8libsite-packagesdashdash.py", line 1031, in dispatch
response.set_data(func(*args, outputs_list=outputs_list))
File "D:SoftwarePython 3.6.8libsite-packagesdashdash.py", line 966, in add_context
output_value = func(*args, **kwargs)  # %% callback invoked %%
File "c:UsersKennyDesktopproducttype.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:SoftwarePython 3.6.8libsite-packagespandascoregeneric.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()

如果我理解正确,您想查找产品名称不相等的行,并替换这些行的product_date_up值吗?你应该可以使用这个,然后不需要for loop

original.loc[(original["Product name"]!=new["Product name"]),"Product_date_up"]=datetime.now() 

相关内容

最新更新