Python 3.x 比较数据帧的OrderDict
我面临一个比较问题,这是我的代码:Python 3.x 比较数据帧的OrderDict,python-3.x,pandas,dictionary,dataframe,compare,Python 3.x,Pandas,Dictionary,Dataframe,Compare,我面临一个比较问题,这是我的代码: import pandas as pd from collections import OrderedDict from pandas.util.testing import assert_frame_equal df1 = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']), 'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c'
import pandas as pd
from collections import OrderedDict
from pandas.util.testing import assert_frame_equal
df1 = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd']),
'three' : pd.Series([1., 2., 3., 4., 5.], index=['a', 'b', 'c', 'd','e'])}
df2 = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd']),
'three' : pd.Series([1., 2., 3., 4., 5.], index=['a', 'b', 'c', 'd','e'])}
od = OrderedDict()
od['a'] = pd.DataFrame(df1)
od['b'] = pd.DataFrame(df1)
od['c'] = pd.DataFrame(df1)
od['d'] = pd.DataFrame(df1)
od2 = OrderedDict()
od2['a'] = pd.DataFrame(df2)
od2['b'] = pd.DataFrame(df2)
od2['c'] = pd.DataFrame(df2)
od2['d'] = pd.DataFrame(df2)
test = assert_frame_equal(od, od2)
print(test)
我有两个由pandas数据帧组成的OrderedDict,我想分析数据帧中的单个元素是否相等
我发现函数assert_frame_equal在比较2个数据帧时非常有效,但在OrderedDict中给出了这个错误:
AssertionError:DataFrame应为类型类
找到类“pandas.core.frame.DataFrame”
改为“collections.OrderedDict”
有什么解决方案或解决办法吗?考虑到我必须从一个有序的信息技术开始,不幸的是,我无法改变这一点
非常感谢您在此问题上提供的任何帮助/提示。看看这是否有帮助。使用
希望这有帮助看看这是否有帮助。使用 希望这能对你有所帮助
for df1, df2 in zip(od.values(), od2.values()):
test = assert_frame_equal(df1, df2)
print(test)
返回:
None
None
None
None
True
True
True
True
另外,您正在调用初始的OrderedDict
sdf1
,这可能会导致以后的一些混淆。按照惯例,df
是DataFrame
的缩写
您还可以根据使用equal
返回:
None
None
None
None
True
True
True
True
像这样的
for df1, df2 in zip(od.values(), od2.values()):
test = assert_frame_equal(df1, df2)
print(test)
返回:
None
None
None
None
True
True
True
True
另外,您正在调用初始的OrderedDict
sdf1
,这可能会导致以后的一些混淆。按照惯例,df
是DataFrame
的缩写
您还可以根据使用equal
返回:
None
None
None
None
True
True
True
True
od
和od2
中的值是数据帧,因此我认为不需要调用构造函数k
和v
是key
和value
的缩写,因此使用k
引用od.values()
可能会让一些人感到困惑。@Evan我同意你的看法。od
和od2
中的值是数据帧,所以我认为不需要调用构造函数k
和v
是key
和value
的简写,因此使用k
引用od.values()
可能会让一些人感到困惑。@Evan我同意你的看法。