6
df = df1.loc[df1['CUST_ACCT_KEY'] != df2['CUST_ACCT_KEY']]

When I execute the above command, I get the following error:

ValueError: Can only compare identically-labeled Series objects

What am I doing wrong?*

The dtypes of both the column are int64.

1
  • 1
    The indexed of df1 and df1 are both resetted before performing the action Commented Jun 27, 2018 at 16:25

1 Answer 1

8

Pandas does almost all of its operations with intrinsic data alignment, meaning it uses indexes to compare, and perform operations.

You could avoid this error by converting one of the series to a numpy array using .values:

df = df1.loc[df1['CUST_ACCT_KEY'] != df2['CUST_ACCT_KEY']].values

However, you are comparing row to row with no index alignment.

MCVE:

df1 = pd.DataFrame(np.arange(1,10), index=np.arange(1,10),columns=['A'])

df2 = pd.DataFrame(np.arange(11,20), index=np.arange(11,20),columns=['B'])

df1['A'] != df2['B']

Output:

ValueError: Can only compare identically-labeled Series objects

Change to numpy array:

df1['A'] != df2['B'].values

Output:

1    True
2    True
3    True
4    True
5    True
6    True
7    True
8    True
9    True
Name: A, dtype: bool
Sign up to request clarification or add additional context in comments.

3 Comments

The indexes of both df1 and df2 were resetted before I perfomed the task. I also tried numpy.where, but yielded the same result
Can you post some data along with code in the question.
I made use of my unique indexes to filter the data. Didn't need to reset the data - df.drop(df_subset.index)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.