简体   繁体   English

Pandas DataFrame相等性-索引编号

[英]Pandas DataFrame equality - index numbering

Does Index numbering matter in testing dataframe equality? 索引编号在测试数据帧是否相等时重要吗? I have 2 identical dataframes with exactly the same data and columns. 我有2个完全相同的数据框和完全相同的数据和列。 The only difference is that the index numbers for each row is different and equals methods returns a False. 唯一的区别是每一行的索引号不同,并且equals方法返回False。 How can I get around this? 我该如何解决? Here are my data frames 这是我的数据框

   A   B
0  87  54
1  87  75
2  87  22
3  87  69

     A   B
418  87  69
107  87  54
108  87  75
250  87  22

You can use np.array_equal to check the values, however the ordering is important, so in your example you have to sort by the index first. 您可以使用np.array_equal来检查值,但是顺序很重要,因此在您的示例中,您必须首先按索引排序。

In [11]: df1
Out[11]:
    A   B
0  87  54
1  87  75
2  87  22
3  87  69

In [12]: df2
Out[12]:
      A   B
418  87  69
107  87  54
108  87  75
250  87  22

In [13]: df3 = df2.sort()

In [14]: df3
Out[14]:
      A   B
107  87  54
108  87  75
250  87  22
418  87  69

In [15]: np.array_equal(df1, df3)
Out[15]: True

Note: You can't compare df1 and df2 as they have different indexes: 注意:您无法比较df1和df2,因为它们具有不同的索引:

In [21]: df1 == df2
ValueError: Can only compare identically-labeled DataFrame object

You can reset the index, but be aware that an exception can be raised for that reason: 您可以重置索引,但是请注意,由于这个原因会引发异常:

In [22]: df3.reset_index(drop=True)
Out[22]:
    A   B
0  87  54
1  87  75
2  87  22
3  87  69

In [23]: np.all(df1 == df3.reset_index(drop=True))
Out[23]: True

Another option is to have a try and except block around assert_frame_equals : 另一个选择是尝试一下,但在assert_frame_equals周围进行assert_frame_equals

In [24]: pd.util.testing.assert_frame_equal(df1, df3.reset_index(drop=True))

as in this related answer . 就像这个相关的答案

As Jeff points out you can use .equals, which does this: 正如Jeff指出的那样,您可以使用.equals,它可以这样做:

In [25]: df1.equals(df3.reset_index(drop=True))
Out[25]: True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM