-4

I have got pandas dataframe, looking like that:

    HomeTeam    AwayTeam    HTR FTR

39  Arsenal     Tottenham   2   12
136 Norwich     Arsenal     1   2
101 Arsenal     Wolves      1   1

I would like to add there column, to compare HTR and FTR in every row. There are 3 possible values: 1,2 or 12.

If HTR=FTR I would like to get "1" in new column, if HTR ≠ FTR I would like to get "0".

rene
  • 41,474
  • 78
  • 114
  • 152
  • 3
    Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – 89f3a1c Sep 01 '20 at 17:31
  • Your question is quite good and clear now but in future - asking questions on pandas you can read this: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples It'll help you to pay attention to your questions and acquire more knowledge from the answers. Have a good evening and welcome to StackOverflow (if you are new here :) ) – Stas Buzuluk Sep 01 '20 at 18:18

1 Answers1

0

Let's generate some artificial data, you described:

df = pd.DataFrame({'HTR': np.random.choice([1,2,12],100),
                   'FTR':np.random.choice([1,2,12],100)})

Dataframe looks like this now:

    HTR FTR

39   2  12
136  1  2
101  1  1

And, now, let's solve your problem:

df["are_same"] = (df["HTR"]==df["FTR"]).astype(int)

After adding of new column Dataframe looks like this:

    HTR FTR are_same

39   2  12  0
136  1  2   0
101  1  1   1
Stas Buzuluk
  • 794
  • 9
  • 19