【问题标题】:Python Version 3 - ValueError: not enough values to unpack (expected 2, got 1)Python 版本 3 - ValueError: no enough values to unpack (expected 2, got 1)
【发布时间】:2020-10-26 21:07:01
【问题描述】:

尝试执行此 excel 比较脚本时遇到上述错误,请帮助。 该脚本的目标是能够比较 excel 并突出显示差异,如果您能建议我如何包含主键之类的列,那就太好了


df1=pd.read_excel('File1.xlsx')
df2=pd.read_excel('File2.xlsx')

df1.equals(df2)

comparison_values = df1.values == df2.values
print (comparison_values)

import numpy as np
rows,cols=np.where(comparison_values==False)

for item in zip(rows,cols):
    df1.iloc[item[0], item[1]] = '{} --> {}'.format(df1.iloc[item[0], item[1]],df2.iloc[item[0], item[1]])

df1.to_excel('./Excel_diff.xlsx',index=False,header=True)


python3 compare_excels.py 
compare_excels.py:8: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
  comparison_values = df1.values == df2.values
False
Traceback (most recent call last):
  File "compare_excels.py", line 12, in <module>
    rows,cols=np.where(comparison_values==False)
ValueError: not enough values to unpack (expected 2, got 1)```

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    在您的示例中,df1df2 似乎都是一维数组。因此,numpy 的 where() 函数返回一个包含一个值的元组,所以当你键入时:

    rows, cols = np.where(comparison_values==False)
    

    您实际上是在尝试将不存在的第二个值分配给 cols。如果你想将返回值分配给rows,你应该有类似的东西:

    rows, = np.where(comparison_values==False) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-10
      • 2017-12-23
      • 2020-11-19
      • 1970-01-01
      • 2022-07-22
      • 2022-11-12
      • 2021-12-31
      • 2021-10-27
      相关资源
      最近更新 更多