Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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 当两个字典相同时,比较它们会返回错误_Python_Pandas_Dictionary_Pickle - Fatal编程技术网

Python 当两个字典相同时,比较它们会返回错误

Python 当两个字典相同时,比较它们会返回错误,python,pandas,dictionary,pickle,Python,Pandas,Dictionary,Pickle,我有一本熊猫dfs字典,我将其转换为如下pickle文件: with open('performance.pkl', 'wb') as handle: pickle.dump(performance, handle, protocol=pickle.HIGHEST_PROTOCOL) with open('performance.pkl', 'rb') as handle: a = pickle.load(handle) 然后我加载pickle文件,如下所示: with op

我有一本熊猫dfs字典,我将其转换为如下pickle文件:

with open('performance.pkl', 'wb') as handle:
    pickle.dump(performance, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open('performance.pkl', 'rb') as handle:
    a = pickle.load(handle)
然后我加载pickle文件,如下所示:

with open('performance.pkl', 'wb') as handle:
    pickle.dump(performance, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open('performance.pkl', 'rb') as handle:
    a = pickle.load(handle)
当我检查字典“performance”和“a”的内容时,它们是相同的,但是,如果我检查:

a == performance
我得到:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
此外:

a.keys() == performance.keys()
True

a.values == performance.values()
False

(type(a), type(performance) 
(dict, dict)
此外,在逐个删除和比较“a”中的DFs和“performance”中的DFs时,它们是相同的

因为我正在比较python字典,所以我不确定问题出在哪里。我不想一个接一个地循环“a”和“performance”中的DFs,因为每个DFs中都有许多索引,这需要时间

顺便说一句,我不需要另存为pickle,但可以使用任何其他格式保存字典。

试试

a.equals(performance)
参考资料-

编辑答案-

for key1 in a.keys():
    if(a[key1].equals(performance[key1]):
        print(True)
    else:
        print(False)

AttributeError:“dict”对象没有属性“equals”。这不是关于比较DFs,而是关于比较包含DFs的字典。当您比较字典时,它仍然会比较dict中存储的任何对象。这可能有助于了解什么是
性能
,什么是
a
?它们是字典还是数据帧?@Quanghaang。对正如问题的标题所说,“比较两个pandas DFs字典…”它们是包含pandas DFs的字典..这样做的原因非常特殊。请尝试打印
a
以查看结果,因为错误显示您正在比较数据帧。
a
是数据帧字典吗?在这种情况下,
df1==df2
将产生上述错误。您需要在a:a[x].eq(性能[x]).all(无)中为x执行一个循环: