首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pandas 数据对比

Pandas 数据对比

作者头像
kirin
发布2021-04-19 16:35:29
4.8K0
发布2021-04-19 16:35:29
举报
文章被收录于专栏:Kirin博客Kirin博客

df.compare() 和s.compare() 方法使您可以分别比较两个DataFrame 或 Series,并总结它们之间的差异。V1.1.0 中添加了此功能。

语法

语法如下:

pd.compare(other, align_axis=1, keep_shape=False, keep_equal=False)

其中:

  • other:被对比的数据
  • align_axis=1:差异堆叠在列/行上
  • keep_shape=False:不保留相等的值
  • keep_equal=False:不保留所有原始行和列

用法

例如,您可能想要比较两个DataFrame并并排堆叠它们的差异。

df = pd.DataFrame(
    {
        "col1": ["a", "a", "b", "b", "a"],
        "col2": [1.0, 2.0, 3.0, np.nan, 5.0],
        "col3": [1.0, 2.0, 3.0, 4.0, 5.0]
    },
    columns=["col1", "col2", "col3"],
)
df
'''
  col1  col2  col3
0    a   1.0   1.0
1    a   2.0   2.0
2    b   3.0   3.0
3    b   NaN   4.0
4    a   5.0   5.0
'''

# 对数据进行修改以便进行对比
df2 = df.copy()
df2.loc[0, 'col1'] = 'c'
df2.loc[2, 'col3'] = 4.0

df2
'''
  col1  col2  col3
0    c   1.0   1.0
1    a   2.0   2.0
2    b   3.0   4.0
3    b   NaN   4.0
4    a   5.0   5.0
'''

应用对比:

df.compare(df2)
'''
  col1       col3
  self other self other
0    a     c  NaN   NaN
2  NaN   NaN  3.0   4.0
'''

默认情况下,如果两个对应的值相等,它们将显示为NaN。 此外,如果整个行/列中的所有值都将从结果中省略。 其余差异将在列上对齐。

其他方法

还可以传入以下参数:

df = pd.DataFrame(
    {
        "col1": ["a", "a", "b", "b", "a"],
        "col2": [1.0, 2.0, 3.0, np.nan, 5.0],
        "col3": [1.0, 2.0, 3.0, 4.0, 5.0]
    },
    columns=["col1", "col2", "col3"],
)

df
'''
  col1  col2  col3
0    a   1.0   1.0
1    a   2.0   2.0
2    b   3.0   3.0
3    b   NaN   4.0
4    a   5.0   5.0
'''

修改数据,方便对比:

df2 = df.copy()
df2.loc[0, 'col1'] = 'c'
df2.loc[2, 'col3'] = 4.0
df2
'''
  col1  col2  col3
0    c   1.0   1.0
1    a   2.0   2.0
2    b   3.0   4.0
3    b   NaN   4.0
4    a   5.0   5.0
'''

显示有差异的列:

df.compare(df2)
'''
  col1       col3
  self other self other
0    a     c  NaN   NaN
2  NaN   NaN  3.0   4.0
'''

将差异堆叠在行上:

df.compare(df2, align_axis=0)
'''
        col1  col3
0 self     a   NaN
  other    c   NaN
2 self   NaN   3.0
  other  NaN   4.0
'''

保留相等的值:

df.compare(df2, keep_equal=True)
'''
  col1       col3
  self other self other
0    a     c  1.0   1.0
2    b     b  3.0   4.0
'''

保留所有原始行和列:

df.compare(df2, keep_shape=True)
'''
  col1       col2       col3
  self other self other self other
0    a     c  NaN   NaN  NaN   NaN
1  NaN   NaN  NaN   NaN  NaN   NaN
2  NaN   NaN  NaN   NaN  3.0   4.0
3  NaN   NaN  NaN   NaN  NaN   NaN
4  NaN   NaN  NaN   NaN  NaN   NaN
'''

保留所有原始行和列以及所有原始值:

df.compare(df2, keep_shape=True, keep_equal=True)
'''
  col1       col2       col3
  self other self other self other
0    a     c  1.0   1.0  1.0   1.0
1    a     a  2.0   2.0  2.0   2.0
2    b     b  3.0   3.0  3.0   4.0
3    b     b  NaN   NaN  4.0   4.0
4    a     a  5.0   5.0  5.0   5.0
'''

数据相同

此外,还可以使用df1.equals(df2)来对比两个数据是否一致,测试两个对象是否包含相同的元素。

此功能允许将两个Series或DataFrame相互比较,以查看它们是否具有相同的形状和元素。 相同位置的NaN被认为是相等的。 列标题不必具有相同的类型,但是列中的元素必须具有相同的dtype。

此功能要求元素与其他Series或DataFrame中的元素具有相同的dtype。 但是,列标签不必具有相同的类型,只要它们仍被视为相等即可。

df = pd.DataFrame({1: [10], 2: [20]})
df
    1   2
0  10  20

DataFrames df和fully_equal的元素和列标签具有相同的类型和值,它们将返回True。

exactly_equal = pd.DataFrame({1: [10], 2: [20]})
exactly_equal
'''
    1   2
0  10  20
'''
df.equals(exactly_equal)
# True

DataFrames df和different_column_type具有相同的元素类型和值,但列标签具有不同的类型,它们仍将返回True。

different_column_type = pd.DataFrame({1.0: [10], 2.0: [20]})
different_column_type
'''
   1.0  2.0
0   10   20
'''
df.equals(different_column_type)
# True

DataFrames df和different_data_type为其元素的相同值具有不同的类型,即使它们的列标签具有相同的值和类型,它们也将返回False。

different_data_type = pd.DataFrame({1: [10.0], 2: [20.0]})
different_data_type
'''
      1     2
0  10.0  20.0
'''
df.equals(different_data_type)
# False
提一嘴,现在新版本的pandas 中可以直接用compare对比了
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-04-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 语法
  • 用法
  • 其他方法
  • 数据相同
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档