Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取错误值错误:序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()_Python_Pandas_Dataframe_Datatable - Fatal编程技术网

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

Python 获取错误值错误:序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all(),python,pandas,dataframe,datatable,Python,Pandas,Dataframe,Datatable,我有两个来自dash数据表的数据帧,第一个是来自数据库的原始数据帧,第二个是来自编辑的数据表 在本例中,我有一列product\u date\u up表示最后编辑的时间戳 我已经生成了一个函数,在用户每次单击保存按钮时添加时间戳,并且时间戳自动插入到已编辑的行中 为此,我正在使用此代码 import sqlalchemy as alch from datetime import datetime @app.callback( Output('table-container-edit',

我有两个来自dash数据表的数据帧,第一个是来自数据库的原始数据帧,第二个是来自编辑的数据表

在本例中,我有一列
product\u date\u 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:\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()
我尝试了
不是
,而是更新整行。我能用什么

编辑

根据Gingerhaze的答案和少量编辑,此代码工作起来非常神奇

df_table.loc[(df_original["product_name"] != df_table["product_name"]),"product_date_up"]=datetime.now()

如果我理解正确,您希望查找产品名称不相等的行,并替换这些行的product_date_up值吗?您应该能够使用它,而不需要for循环

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

请提供完整的回溯,而不仅仅是错误。嗨,我已经编辑了@it\u is\u Chris