简体   繁体   中英

No error for lists of unequal length compared

Given a list of dataframes I am trying to find if I can merge similar tables.

dummy_data1 = {
        'id': ['1', '2', '3', '4', '5'],
        'Feature1': ['A', 'C', 'E', 'G', 'I'],
        'Feature2': ['B', 'D', 'F', 'H', 'J']}

dummy_data2 = {
        'id': ['1', '2', '6', '7', '8'],
        'Feature1': ['K', 'M', 'O', 'Q', 'S'],
        'Feature2': ['L', 'N', 'P', 'R', 'T']}

dummy_data3 = {
        'id': ['1', '2', '6', '7', '8'],
        'Feature1': ['R', 'S', 'T', 'U', 'V']}

df1 = pd.DataFrame(dummy_data1, columns = ['id', 'Feature1', 'Feature2'])
df2 = pd.DataFrame(dummy_data2, columns = ['id', 'Feature1', 'Feature2'])
df3 = pd.DataFrame(dummy_data3, columns = ['id', 'Feature1'])

This operation(see code below) is very well understood(No error in case 1 and length mismatch in case 2)

>>>df1.columns == df2.columns # CASE 1
>>>array([ True,  True,  True])

>>>df1.columns == df3.columns # CASE 2
>>>ValueError                                
   Traceback (most recent call last)
   <ipython-input-107-d30deec2e5d5> in <module>
   ----> 1 df1.columns == df3.columns
   ValueError: Lengths must match to compare

But when I'm doing

>>>mergeRequired = False if False in df1.columns == df3.columns else True
>>>print(mergeRequired)
>>>True

I should get ValueError: Lengths must match to compare instead of True . Why is this not throwing any error ? How can I force the program to throw error at mergeRequired = False if False in df1.columns == df3.columns else True ?

When you write this code:

mergeRequired = False if False in df1.columns == df3.columns else True

It is interpreted in the same way as this one:

mergeRequired = False if (False in df1.columns) == df3.columns else True

because the operators in and == have the same precedence in Python (see here ), which explains the observed behavior.

To fix your problem, you should use parentheses as follows:

mergeRequired = False if False in (df1.columns == df3.columns) else True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM