Can only compare identically labeled DataFrame objects with compare?

值错误:只能比较标记相同的DataFrame对象
很明显,我们不可以用不同大小的两个DataFrame,如下两段代码:

data= pd.DataFrame({
    "Nowcoder_ID2":[True,False,True,False],
})
data2= pd.DataFrame({
    "Nowcoder_ID":[True,True,True,False]
})
data==data2
data= pd.DataFrame({
    "Nowcoder_ID2":[True,False,True,False],

})
data2= pd.DataFrame({
     "Nowcoder_ID2":[True,True,True,False,False]
})
data==data2

       两者行、列向量必须相同,否则就会报此错误,即有关不同行、列向量不匹配的两个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 = "http://ichart.finance.yahoo.com/table.csv?s=MSFT"
blobUrlBase = "https://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:

import pandas as pd

#define DataFrames
df1 = pd.DataFrame({'points': [25, 12, 15, 14],
                   'assists': [5, 7, 13, 12]})

df2 = pd.DataFrame({'points': [25, 12, 15, 14],
                    'assists': [5, 7, 13, 12]},
                     index=[3, 2, 1, 0])

#view DataFrames
print(df1)

   points  assists
0      25        5
1      12        7
2      15       13
3      14       12

print(df2)

   points  assists
3      25        5
2      12        7
1      15       13
0      14       12

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 objects

How 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:

Which method is used to compare two DataFrames?

The compare method in pandas shows the differences between two DataFrames. It compares two data frames, row-wise and column-wise, and presents the differences side by side. The compare method can only compare DataFrames of the same shape, with exact dimensions and identical row and column labels.

How do you compare values between two data frames?

Here are the steps for comparing values in two pandas Dataframes:.
Step 1 Dataframe Creation: The dataframes for the two datasets can be created using the following code:.
Output:.
Step 2 Comparison of values: You need to import numpy for the successful execution of this step..

How do you compare values of two different DataFrames in pandas?

Pandas compare columns in two data frames.
Create two dataframes - df1 and df2..
Concatenate two dataframes side by side..
Compare the column in two dataframes on a common key..
Additionally, find the matching rows between two dataframe..
find the non-matching rows between the dataframes..

How do you check if two DataFrames are identical in Python?

Pandas DataFrame equals() Method The duplicated() method compares two DataFrames and returns True if they are equal, in both shape and content, otherwise False.