有没有一种简单的方法来检查两个数据帧是否是相同底层数据的不同副本或视图,而不涉及操作?我正在试图掌握每个规则是什么时候生成的,并且考虑到这些规则看起来是多么独特,我希望有一种简单的方法来测试。
例如,我认为"id(df.values)“在视图之间应该是稳定的,但它们似乎不是:
# Make two data frames that are views of same data.
df = pd.DataFrame([[1,2,3,4],[5,6,7,8]], index = ['row1','row2'],
columns = ['a','b','c','d'])
df2 = df.iloc[0:2,:]
# Demonstrate they are views:
df.iloc[0,0] = 99
df2.iloc[0,0]
Out[70]: 99
# Now try and compare the id on values attribute
# Different despite being views!
id(df.values)
Out[71]: 4753564496
id(df2.values)
Out[72]: 4753603728
# And we can of course compare df and df2
df is df2
Out[73]: False
我查找的其他答案试图给出规则,但似乎不一致,也没有回答如何测试的问题:
当然还有:- http://pandas.pydata.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy
更新:下面的注释似乎回答了这个问题--查看df.values.base
属性而不是df.values
属性,以及对df._is_copy
属性的引用(尽管后者可能是非常糟糕的形式,因为它是内部属性)。
https://stackoverflow.com/questions/26879073
复制相似问题