PyPackage01---Pandas13_比较两个df是否一致

Intro

检查两个df是否完全一致

import pandas as pd
from pandas.testing import assert_frame_equal
x1=pd.DataFrame({"x1":[1,2],"x2":[2,3]})
x2=pd.DataFrame({"x1":[1,2],"x2":[2,3]})
x3=pd.DataFrame({"x1":[1,2],"x2":[2,4]})
def is_df_equal(df1,df2):
    try:
        assert_frame_equal(df1, df2)
        print("两个df一致")
    except Exception as e:
        print("出现如下异常:\n%s"%e)
is_df_equal(df1=x1, df2=x2)
两个df一致
is_df_equal(df1=x1, df2=x3)
出现如下异常:
DataFrame.iloc[:, 1] (column name="x2") are different

DataFrame.iloc[:, 1] (column name="x2") values are different (50.0 %)
[index]: [0, 1]
[left]:  [2, 3]
[right]: [2, 4]

值得注意的是,如果两个df的index不同也会报错,看个例子

x4=pd.DataFrame({"x1":[1,2],"x2":[2,3]})
x4.index = [0,3]
x4
x1 x2
0 1 2
3 2 3
is_df_equal(df1=x1, df2=x4)
出现如下异常:
DataFrame.index are different

DataFrame.index values are different (50.0 %)
[left]:  RangeIndex(start=0, stop=2, step=1)
[right]: Int64Index([0, 3], dtype='int64')

介绍简单用法,再比较数据是否一样时,可以直接用

                                2021-11-18 于南京市江宁区九龙湖

上一篇:httprunner1.x 断言-equal


下一篇:数据分析之Numpy常用方法【array_equal()、unique()】