在几种情况下比较两个 pandas 数据帧是否相等
Compare two pandas dataframes for equality, under several conditions
R 中 compare
包的 Python pandas 等价物是什么,它包含一个同名函数,在几个方面比较两个数据集:
- 您可以将不同的参数传递给
compare()
函数以指定应接受 df1 和 df2 之间的哪些差异。
- 如果两个数据集相等,但是因为覆盖的时间段比较长,所以可以设置
short=TRUE
.
可能参数的完整列表(在 R 中)是:
compare(model, comparison,
equal = TRUE,
coerce = allowAll,
shorten = allowAll,
ignoreOrder = allowAll,
ignoreNameCase = allowAll,
ignoreNames = allowAll,
ignoreAttrs = allowAll,
round = FALSE,
ignoreCase = allowAll,
trim = allowAll,
dropLevels = allowAll,
ignoreLevelOrder = allowAll,
ignoreDimOrder = allowAll,
ignoreColOrder = allowAll,
ignoreComponentOrder = allowAll,
colsOnly = !allowAll,
allowAll = FALSE)
Python 是否有类似的包,可以处理两个数据集之间不同和相等的不同情况。我还没有找到与 R 的 compare
函数等效的函数。
我发现最接近 compare
的是 pandas.testing.assert_frame_equal(df, expected, check_names=False)
函数,它不如 compare() 广泛。
在与 Python 合作几个月后,我可以提出自己问题的答案。
比较两个数组使用numpy包中的函数。
numpy.array_equal(array1,array2) # test if the same shape and values
numpy.array_equiv(array1,array2) # test if shape consistent and all elements equal
numpy.allclose(array1,array2) # test if same shape, elements have close values
可以找到这些函数的文档 here。
这就是我现在所需要的,但也许它对某些人有帮助:
要比较两个文本序列,请使用difflib 包,它有助于比较HTML、.txt 文件等。可以阅读文档here.
R 中 compare
包的 Python pandas 等价物是什么,它包含一个同名函数,在几个方面比较两个数据集:
- 您可以将不同的参数传递给
compare()
函数以指定应接受 df1 和 df2 之间的哪些差异。 - 如果两个数据集相等,但是因为覆盖的时间段比较长,所以可以设置
short=TRUE
.
可能参数的完整列表(在 R 中)是:
compare(model, comparison,
equal = TRUE,
coerce = allowAll,
shorten = allowAll,
ignoreOrder = allowAll,
ignoreNameCase = allowAll,
ignoreNames = allowAll,
ignoreAttrs = allowAll,
round = FALSE,
ignoreCase = allowAll,
trim = allowAll,
dropLevels = allowAll,
ignoreLevelOrder = allowAll,
ignoreDimOrder = allowAll,
ignoreColOrder = allowAll,
ignoreComponentOrder = allowAll,
colsOnly = !allowAll,
allowAll = FALSE)
Python 是否有类似的包,可以处理两个数据集之间不同和相等的不同情况。我还没有找到与 R 的 compare
函数等效的函数。
我发现最接近 compare
的是 pandas.testing.assert_frame_equal(df, expected, check_names=False)
函数,它不如 compare() 广泛。
在与 Python 合作几个月后,我可以提出自己问题的答案。
比较两个数组使用numpy包中的函数。
numpy.array_equal(array1,array2) # test if the same shape and values
numpy.array_equiv(array1,array2) # test if shape consistent and all elements equal
numpy.allclose(array1,array2) # test if same shape, elements have close values
可以找到这些函数的文档 here。
这就是我现在所需要的,但也许它对某些人有帮助: 要比较两个文本序列,请使用difflib 包,它有助于比较HTML、.txt 文件等。可以阅读文档here.