vendredi 1 février 2019

Compare two columns using pandas 2

I'm comparing two columns in a dataframe (A & B). I have a method that works (C5). It came from this question: Compare two columns using pandas

I wondered why I couldn't get the other methods (C1 - C4) to give the correct answer:

df = pd.DataFrame({'A': [1,1,1,1,1,2,2,2,2,2],
                   'B': [1,1,1,1,1,1,0,0,0,0]})

#df['C1'] = 1 [df['A'] == df['B']]

df['C2'] = df['A'].equals(df['B'])

df['C3'] = np.where((df['A'] == df['B']),0,1)

def fun(row):
    if ['A'] == ['B']:
        return 1
    else:
        return 0
df['C4'] = df.apply(fun, axis=1)

df['C5'] = df.apply(lambda x : 1 if x['A'] == x['B'] else 0, axis=1)

enter image description here

Aucun commentaire:

Enregistrer un commentaire