值错误:只能比较标记相同的DataFrame对象
很明显,我们不可以用不同大小的两个DataFrame,如下两段代码:
两者行、列向量必须相同,否则就会报此错误,即有关不同行、列向量不匹配的两个DataFrame布恩那个是用 == 或者 !=符号。
data= pd.DataFrame({ "Nowcoder_ID2":[True,False,True,False], }) data2= pd.DataFrame({ "Nowcoder_ID2":[True,True,True,False] }) data.index=[2,3,4,5] data==data2同样,索引不同也会产生此错误,我在使用concat()合并以后不小心多运行了一次,然后重复合并,看了好几遍逻辑也没发现错误…所以前后变量能不用同一个就不用一个😂😂
I tried all the solutions here: Pandas "Can only compare identically-labeled DataFrame objects" error
Didn't work for me. Here's what I've got. I have two data frames. One is a set of financial data that already exists in the system and another set that has some that may or may not exist in the system. I need to find the difference and add the stuff that doesn't exist.
Here is the code:
import pandas as pd import numpy as np from azure.storage.blob import AppendBlobService, PublicAccess, ContentSettings from io import StringIO dataUrl = "//ichart.finance.yahoo.com/table.csv?s=MSFT" blobUrlBase = "//pyjobs.blob.core.windows.net/" data = pd.read_csv(dataUrl) abs = AppendBlobService(account_name='pyjobs', account_key='***') abs.create_container("stocks", public_access = PublicAccess.Container) abs.append_blob_from_text('stocks', 'msft', data[:25].to_csv(index=False)) existing = pd.read_csv(StringIO(abs.get_blob_to_text('stocks', 'msft').content)) ne = (data != existing).any(1)the failing code is the final line. I was going through an article on determining differences between data frames.
I checked the dtypes on all columns, they appear to be the same. I also did a side by side output, I sorted teh axis, the indices, dropped the indices etc. Still get that bloody error.
This error occurs when you attempt to compare two pandas DataFrames and either the index labels or the column labels do not perfectly match.The following example shows how to fix this error in practice.
How to Reproduce the Error
Suppose we have the following two pandas DataFrames:
Notice that the column labels match, but the index labels do not.
If we attempt to compare the two DataFrames, we’ll receive an error:
#attempt to compare the DataFrames df1 = df2 ValueError: Can only compare identically-labeled DataFrame objectsHow to Fix the Error
There are a few methods we can use to address this error.
Method 1: Compare DataFrames (including index labels)
We can use the following syntax to compare the two DataFrames row by row to see which row values match: