resultBool = (results_01 != results_02).stack()
resultdiff = pd.concat([results_01.stack()[resultBool], results_02.stack()[resultBool]], axis=1)
resultdiff.columns=["output_01", "output_02"]
我在网上找到了这段代码,用于比较两个csv文件并打印它们的区别,我不太清楚前两行后面的逻辑,有人能解释一下吗,非常感谢。你知道吗
Tags:
例如,让我们考虑下面两个新创建的数据帧:
它们看起来像这样:
not equal to表达式
!=
只返回一个新的df,其中值不相等。你知道吗stack
用新索引重塑数据帧。更多信息here。你知道吗上面的行将结果df(如第二张图片所示)保存在一个变量中。你知道吗
下面一行从原始数据帧中过滤掉所有假值。你知道吗
注意索引
2 B
是如何丢失的,因为resultBool
df中的值是false。你知道吗这意味着这个特定单元格的df1和df2中的值相等,因此
!=
运算符的结果为false。你知道吗我们对df2也做了同样的工作,只是水平地连接结果。有关熊猫连接here的详细信息。你知道吗
在上图中,列“0”引用df1中的值,列“1”引用df2中的值。你知道吗
最后,我们将这些列重命名为“output\u 01”和“output\u 02”:
最终结果是一个新的df高亮显示两个数据帧中不同的值。你知道吗
resultBool
是DataFrame.stack()
的结果,它将名为result_01
和result_02
的两个csv文件的结果分解为True
和False
值的单数列,列名现在被堆叠为MultiIndex
,表示两者之间是否存在差异,True
表示自results_01 != results_02
以来的差异。你知道吗resultdiff
通过传入resultBool
水平地执行pd.concat
,这只会保存前面提到的resultBool
的结果。你知道吗之后,
resultdiff
设置列。你知道吗有关更多信息,您应该查看所涉及的函数,以及在每行代码之后打印数据帧,以查看发生了什么。你知道吗
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.stack.html
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html
相关问题 更多 >
编程相关推荐