-1

When checking element-wise equality of multiple columns of a dataframe against a single column, pandas raises a ValueError: Operands are not aligned. Do 'left, right = left.align(right, axis=1, copy=False)' before operating..

import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'B': [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 'C': [21, 22, 23, 24, 25, 26, 27, 28, 29, 30] }) df2 = pd.DataFrame({ 'X': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] }) cond1 = (df1['A'] == df2['X']) cond2 = (df1[['A','B','C']] == 5) cond3 = (df1[['A','B','C']] == df2['X']) # raises an alignment error cond3 = (df1[['A','B','C']] == df1['A']) # raises an alignment error 

Why does pandas raise this error? I would have assumed that pandas performs an element-wise comparison without issues, either aligning on the existing index of the columns (which is the same between the dataframes) or on an ad-hoc-assigned new index (from 0 to N-1).

Is there a way to avoid the left.align() suggestion without converting to numpy arrays as shown below?

cond3 = (df1[['A','B','C']].values == df2['X'].values[:,None]) 
0

2 Answers 2

1

DataFrame == Series will try to align the Series' index with the DataFrame columns.

You should use eq with axis=0:

df1[['A','B','C']].eq(df2['X'], axis=0) 

Output:

 A B C 0 False False False 1 False False False 2 False False False 3 False False False 4 False False False 5 False False False 6 False False False 7 False False False 8 False False False 9 False False False 

Example of the default alignment with ==:

pd.DataFrame({'A': [0,1], 'B': [2,3]}) == pd.Series({'A': 0, 'B': 3}) A B 0 True False 1 False True 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use DataFrame.eq with axis=0, because default parameter is axis=1:

cond3 = (df1[['A','B','C']].eq(df2['X'], axis=0)) cond3 = (df1[['A','B','C']].eq(df1['A'], axis=0)) 

1 Comment

that won't work with the default parameter, it should be axis=0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.