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])