user16304089
user16304089

Reputation:

Python When match two columns then

What is the equivalent of this code

df_train['uf'] = (df_train['home_score'] + df_train['away_score'] < 4) * 1

for

df_train['d'] = (df_train['home_score'] = df_train['away_score']) * 1

When True = 1 when False = 0

Upvotes: 0

Views: 72

Answers (2)

Dragan Kojić
Dragan Kojić

Reputation: 66

I am not sure what you want exactly... I think you got mistake on match condition on second code block. Please try this one.

df_train['d'] = (df_train['home_score'] == df_train['away_score']).astype(int)

If it doesn't sth what you expect, please share more details. Thanks

Upvotes: 0

mozway
mozway

Reputation: 260410

The comparison operator is == not =:

df_train['d'] = (df_train['home_score'] == df_train['away_score']).astype(int)

or:

df_train['d'] = df_train['home_score'].eq(df_train['away_score']).astype(int)

or:

df.eval('home_score == away_score').astype(int)

Upvotes: 3

Related Questions