首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使Pandas DataFrames看起来完全相同但失败等于()呢?

如何使Pandas DataFrames看起来完全相同但失败等于()呢?
EN

Stack Overflow用户
提问于 2015-03-27 23:13:26
回答 1查看 5.4K关注 0票数 8

为了确认我理解Pandas df.groupby()df.reset_index()所做的事情,我尝试从dataframe往返到相同数据的分组版本并返回。在往返之后,必须再次对列和行进行排序,因为groupby()会影响行顺序,而reset_index()会影响列顺序,但经过两次快速操作,将列和索引重新排序后,数据流看起来是相同的:

  • 相同的列名列表。
  • 对于每一列都有相同的dtype。
  • 相应的索引值严格相等。
  • 相应的数据值严格相等。

然而,在所有这些检查成功后,df1.equals(df5)返回惊人的值False

equals()发现的这些数据文件之间有什么区别,我还没有弄清楚如何自己检查呢?

测试代码:

代码语言:javascript
运行
复制
csv_text = """\
Title,Year,Director
North by Northwest,1959,Alfred Hitchcock
Notorious,1946,Alfred Hitchcock
The Philadelphia Story,1940,George Cukor
To Catch a Thief,1955,Alfred Hitchcock
His Girl Friday,1940,Howard Hawks
"""

import pandas as pd

df1 = pd.read_csv('sample.csv')
df1.columns = map(str.lower, df1.columns)
print(df1)

df2 = df1.groupby(['director', df1.index]).first()
df3 = df2.reset_index('director')
df4 = df3[['title', 'year', 'director']]
df5 = df4.sort_index()
print(df5)

print()
print(repr(df1.columns))
print(repr(df5.columns))
print()
print(df1.dtypes)
print(df5.dtypes)
print()
print(df1 == df5)
print()
print(df1.index == df5.index)
print()
print(df1.equals(df5))

运行脚本时收到的输出是:

代码语言:javascript
运行
复制
                    title  year          director
0      North by Northwest  1959  Alfred Hitchcock
1               Notorious  1946  Alfred Hitchcock
2  The Philadelphia Story  1940      George Cukor
3        To Catch a Thief  1955  Alfred Hitchcock
4         His Girl Friday  1940      Howard Hawks
                    title  year          director
0      North by Northwest  1959  Alfred Hitchcock
1               Notorious  1946  Alfred Hitchcock
2  The Philadelphia Story  1940      George Cukor
3        To Catch a Thief  1955  Alfred Hitchcock
4         His Girl Friday  1940      Howard Hawks

Index(['title', 'year', 'director'], dtype='object')
Index(['title', 'year', 'director'], dtype='object')

title       object
year         int64
director    object
dtype: object
title       object
year         int64
director    object
dtype: object

  title  year director
0  True  True     True
1  True  True     True
2  True  True     True
3  True  True     True
4  True  True     True

[ True  True  True  True  True]

False

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-27 23:46:42

对我来说,这感觉就像一只虫子,但可能只是我误解了一些东西。这些区块按不同的顺序列出:

代码语言:javascript
运行
复制
>>> df1._data
BlockManager
Items: Index(['title', 'year', 'director'], dtype='object')
Axis 1: Int64Index([0, 1, 2, 3, 4], dtype='int64')
IntBlock: slice(1, 2, 1), 1 x 5, dtype: int64
ObjectBlock: slice(0, 4, 2), 2 x 5, dtype: object
>>> df5._data
BlockManager
Items: Index(['title', 'year', 'director'], dtype='object')
Axis 1: Int64Index([0, 1, 2, 3, 4], dtype='int64')
ObjectBlock: slice(0, 4, 2), 2 x 5, dtype: object
IntBlock: slice(1, 2, 1), 1 x 5, dtype: int64

core/internals.py中,我们有BlockManager方法

代码语言:javascript
运行
复制
def equals(self, other):
    self_axes, other_axes = self.axes, other.axes
    if len(self_axes) != len(other_axes):
        return False
    if not all (ax1.equals(ax2) for ax1, ax2 in zip(self_axes, other_axes)):
        return False
    self._consolidate_inplace()
    other._consolidate_inplace()
    return all(block.equals(oblock) for block, oblock in
               zip(self.blocks, other.blocks))

最后一个all假设selfother中的块对应。但是,如果在它之前添加一些print调用,我们会看到:

代码语言:javascript
运行
复制
>>> df1.equals(df5)
blocks self: (IntBlock: slice(1, 2, 1), 1 x 5, dtype: int64, ObjectBlock: slice(0, 4, 2), 2 x 5, dtype: object)
blocks other: (ObjectBlock: slice(0, 4, 2), 2 x 5, dtype: object, IntBlock: slice(1, 2, 1), 1 x 5, dtype: int64)
False

所以我们在比较错误的东西。我不确定这是否是一个bug的原因是因为我不确定equals是否意味着这么挑剔。如果是这样的话,我认为至少有一个文档错误,因为equals应该说它不打算用于您可能认为的名称和docstring中的内容。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29311659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档