# FIFA World Cup - Benjamin
The notebook is seperated into two parts: a descriptive one and the fair play analysis.

```python
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

plt.rcParams.update({'font.size': 16})

#G=Goal, OG=Own Goal, Y=Yellow Card, R=Red Card, SY = Red Card by second yellow, P=Penalty, MP=Missed Penalty, I = Substitution In, O=Substitute Out, IH= In half time?

df_matches=pd.read_csv('data_raw/WorldCupMatches.csv', sep=',')
df_players=pd.read_csv('data_raw/WorldCupPlayers.csv', sep=',')
df_cups=pd.read_csv('data_raw/WorldCups.csv', sep=',')

df_events = pd.read_csv('data_prepared/event.csv', sep=',').replace(np.nan, '', regex=True)
```

```python
df_matches
```

      Year
      Datetime
      Stage
      Stadium
      City
      Home Team Name
      Home Team Goals
      Away Team Goals
      Away Team Name
      Win conditions
      Attendance
      Half-time Home Goals
      Half-time Away Goals
      Referee
      Assistant 1
      Assistant 2
      RoundID
      MatchID
      Home Team Initials
      Away Team Initials

      0
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico

      4444.0
      3
      0
      LOMBARDI Domingo (URU)
      CRISTOPHE Henry (BEL)
      REGO Gilberto (BRA)
      201
      1096
      FRA
      MEX

      1
      1930
      13 Jul 1930 - 15:00
      Group 4
      Parque Central
      Montevideo
      USA
      3
      0
      Belgium

      18346.0
      2
      0
      MACIAS Jose (ARG)
      MATEUCCI Francisco (URU)
      WARNKEN Alberto (CHI)
      201
      1090
      USA
      BEL

      2
      1930
      14 Jul 1930 - 12:45
      Group 2
      Parque Central
      Montevideo
      Yugoslavia
      2
      1
      Brazil

      24059.0
      2
      0
      TEJADA Anibal (URU)
      VALLARINO Ricardo (URU)
      BALWAY Thomas (FRA)
      201
      1093
      YUG
      BRA

      3
      1930
      14 Jul 1930 - 14:50
      Group 3
      Pocitos
      Montevideo
      Romania
      3
      1
      Peru

      2549.0
      1
      0
      WARNKEN Alberto (CHI)
      LANGENUS Jean (BEL)
      MATEUCCI Francisco (URU)
      201
      1098
      ROU
      PER

      4
      1930
      15 Jul 1930 - 16:00
      Group 1
      Parque Central
      Montevideo
      Argentina
      1
      0
      France

      23409.0
      0
      0
      REGO Gilberto (BRA)
      SAUCEDO Ulises (BOL)
      RADULESCU Constantin (ROU)
      201
      1085
      ARG
      FRA

      5
      1930
      16 Jul 1930 - 14:45
      Group 1
      Parque Central
      Montevideo
      Chile
      3
      0
      Mexico

      9249.0
      1
      0
      CRISTOPHE Henry (BEL)
      APHESTEGUY Martin (URU)
      LANGENUS Jean (BEL)
      201
      1095
      CHI
      MEX

      6
      1930
      17 Jul 1930 - 12:45
      Group 2
      Parque Central
      Montevideo
      Yugoslavia
      4
      0
      Bolivia

      18306.0
      0
      0
      MATEUCCI Francisco (URU)
      LOMBARDI Domingo (URU)
      WARNKEN Alberto (CHI)
      201
      1092
      YUG
      BOL

      7
      1930
      17 Jul 1930 - 14:45
      Group 4
      Parque Central
      Montevideo
      USA
      3
      0
      Paraguay

      18306.0
      2
      0
      MACIAS Jose (ARG)
      APHESTEGUY Martin (URU)
      TEJADA Anibal (URU)
      201
      1097
      USA
      PAR

      8
      1930
      18 Jul 1930 - 14:30
      Group 3
      Estadio Centenario
      Montevideo
      Uruguay
      1
      0
      Peru

      57735.0
      0
      0
      LANGENUS Jean (BEL)
      BALWAY Thomas (FRA)
      CRISTOPHE Henry (BEL)
      201
      1099
      URU
      PER

      9
      1930
      19 Jul 1930 - 12:50
      Group 1
      Estadio Centenario
      Montevideo
      Chile
      1
      0
      France

      2000.0
      0
      0
      TEJADA Anibal (URU)
      LOMBARDI Domingo (URU)
      REGO Gilberto (BRA)
      201
      1094
      CHI
      FRA

      10
      1930
      19 Jul 1930 - 15:00
      Group 1
      Estadio Centenario
      Montevideo
      Argentina
      6
      3
      Mexico

      42100.0
      3
      1
      SAUCEDO Ulises (BOL)
      ALONSO Gualberto (URU)
      RADULESCU Constantin (ROU)
      201
      1086
      ARG
      MEX

      11
      1930
      20 Jul 1930 - 13:00
      Group 2
      Estadio Centenario
      Montevideo
      Brazil
      4
      0
      Bolivia

      25466.0
      1
      0
      BALWAY Thomas (FRA)
      MATEUCCI Francisco (URU)
      VALLEJO Gaspar (MEX)
      201
      1091
      BRA
      BOL

      12
      1930
      20 Jul 1930 - 15:00
      Group 4
      Estadio Centenario
      Montevideo
      Paraguay
      1
      0
      Belgium

      12000.0
      1
      0
      VALLARINO Ricardo (URU)
      MACIAS Jose (ARG)
      LOMBARDI Domingo (URU)
      201
      1089
      PAR
      BEL

      13
      1930
      21 Jul 1930 - 14:50
      Group 3
      Estadio Centenario
      Montevideo
      Uruguay
      4
      0
      Romania

      70022.0
      4
      0
      REGO Gilberto (BRA)
      WARNKEN Alberto (CHI)
      SAUCEDO Ulises (BOL)
      201
      1100
      URU
      ROU

      14
      1930
      22 Jul 1930 - 14:45
      Group 1
      Estadio Centenario
      Montevideo
      Argentina
      3
      1
      Chile

      41459.0
      2
      1
      LANGENUS Jean (BEL)
      CRISTOPHE Henry (BEL)
      SAUCEDO Ulises (BOL)
      201
      1084
      ARG
      CHI

      15
      1930
      26 Jul 1930 - 14:45
      Semi-finals
      Estadio Centenario
      Montevideo
      Argentina
      6
      1
      USA

      72886.0
      1
      0
      LANGENUS Jean (BEL)
      VALLEJO Gaspar (MEX)
      WARNKEN Alberto (CHI)
      202
      1088
      ARG
      USA

      16
      1930
      27 Jul 1930 - 14:45
      Semi-finals
      Estadio Centenario
      Montevideo
      Uruguay
      6
      1
      Yugoslavia

      79867.0
      3
      1
      REGO Gilberto (BRA)
      SAUCEDO Ulises (BOL)
      BALWAY Thomas (FRA)
      202
      1101
      URU
      YUG

      17
      1930
      30 Jul 1930 - 14:15
      Final
      Estadio Centenario
      Montevideo
      Uruguay
      4
      2
      Argentina

      68346.0
      1
      2
      LANGENUS Jean (BEL)
      SAUCEDO Ulises (BOL)
      CRISTOPHE Henry (BEL)
      405
      1087
      URU
      ARG

      18
      1934
      27 May 1934 - 16:30
      Preliminary round
      Stadio Benito Mussolini
      Turin
      Austria
      3
      2
      France
      Austria win after extra time
      16000.0
      0
      0
      VAN MOORSEL Johannes (NED)
      CAIRONI Camillo (ITA)
      BAERT Louis (BEL)
      204
      1104
      AUT
      FRA

      19
      1934
      27 May 1934 - 16:30
      Preliminary round
      Giorgio Ascarelli
      Naples
      Hungary
      4
      2
      Egypt

      9000.0
      2
      2
      BARLASSINA Rinaldo (ITA)
      DATTILO Generoso (ITA)
      SASSI Otello (ITA)
      204
      1119
      HUN
      EGY

      20
      1934
      27 May 1934 - 16:30
      Preliminary round
      San Siro
      Milan
      Switzerland
      3
      2
      Netherlands

      33000.0
      2
      1
      EKLIND Ivan (SWE)
      BERANEK Alois (AUT)
      BONIVENTO Ferruccio (ITA)
      204
      1133
      SUI
      NED

      21
      1934
      27 May 1934 - 16:30
      Preliminary round
      Littorale
      Bologna
      Sweden
      3
      2
      Argentina

      14000.0
      1
      1
      BRAUN Eugen (AUT)
      CARRARO Albino (ITA)
      TURBIANI Giuseppe (ITA)
      204
      1102
      SWE
      ARG

      22
      1934
      27 May 1934 - 16:30
      Preliminary round
      Giovanni Berta
      Florence
      Germany
      5
      2
      Belgium

      8000.0
      1
      2
      MATTEA Francesco (ITA)
      MELANDRI Ermenegildo (ITA)
      BAERT Jacques (FRA)
      204
      1108
      GER
      BEL

      23
      1934
      27 May 1934 - 16:30
      Preliminary round
      Luigi Ferraris
      Genoa
      Spain
      3
      1
      Brazil

      21000.0
      3
      0
      BIRLEM Alfred (GER)
      CARMINATI Ettore (ITA)
      IVANCSICS Mihaly (HUN)
      204
      1111
      ESP
      BRA

      24
      1934
      27 May 1934 - 16:30
      Preliminary round
      Nazionale PNF
      Rome
      Italy
      7
      1
      USA

      25000.0
      3
      0
      MERCET Rene (SUI)
      ESCARTIN Pedro (ESP)
      ZENISEK Bohumil (TCH)
      204
      1135
      ITA
      USA

      25
      1934
      27 May 1934 - 16:30
      Preliminary round
      Littorio
      Trieste
      Czechoslovakia
      2
      1
      Romania

      9000.0
      0
      1
      LANGENUS Jean (BEL)
      SCARPI Giuseppe (ITA)
      SCORZONI Raffaele (ITA)
      204
      1141
      TCH
      ROU

      26
      1934
      31 May 1934 - 16:30
      Quarter-finals
      Stadio Benito Mussolini
      Turin
      Czechoslovakia
      3
      2
      Switzerland

      12000.0
      1
      1
      BERANEK Alois (AUT)
      MOHAMED Youssuf (EGY)
      BAERT Jacques (FRA)
      418
      1143
      TCH
      SUI

      27
      1934
      31 May 1934 - 16:30
      Quarter-finals
      San Siro
      Milan
      Germany
      2
      1
      Sweden

      3000.0
      0
      0
      BARLASSINA Rinaldo (ITA)
      MERCET Rene (SUI)
      VAN MOORSEL Johannes (NED)
      418
      1129
      GER
      SWE

      28
      1934
      31 May 1934 - 16:30
      Quarter-finals
      Giovanni Berta
      Florence
      Italy
      1
      1
      Spain

      35000.0
      0
      0
      BAERT Louis (BEL)
      ZENISEK Bohumil (TCH)
      IVANCSICS Mihaly (HUN)
      418
      1122
      ITA
      ESP

      29
      1934
      31 May 1934 - 16:30
      Quarter-finals
      Littorale
      Bologna
      Austria
      2
      1
      Hungary

      23000.0
      1
      0
      MATTEA Francesco (ITA)
      ESCARTIN Pedro (ESP)
      BIRLEM Alfred (GER)
      418
      1106
      AUT
      HUN

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      806
      2014
      23 Jun 2014 - 17:00
      Group A
      Estadio Nacional
      Brasilia
      Cameroon
      1
      4
      Brazil

      69112.0
      1
      2
      ERIKSSON Jonas (SWE)
      KLASENIUS Mathias (SWE)
      WARNMARK Daniel (SWE)
      255931
      300186472
      CMR
      BRA

      807
      2014
      23 Jun 2014 - 17:00
      Group A
      Arena Pernambuco
      Recife
      Croatia
      1
      3
      Mexico

      41212.0
      0
      0
      Ravshan IRMATOV (UZB)
      RASULOV Abduxamidullo (UZB)
      KOCHKAROV Bakhadyr (KGZ)
      255931
      300186452
      CRO
      MEX

      808
      2014
      24 Jun 2014 - 13:00
      Group D
      Estadio das Dunas
      Natal
      Italy
      0
      1
      Uruguay

      39706.0
      0
      0
      RODRIGUEZ Marco (MEX)
      TORRENTERA Marvin (MEX)
      QUINTERO Marcos (MEX)
      255931
      300186465
      ITA
      URU

      809
      2014
      24 Jun 2014 - 13:00
      Group D
      Estadio Mineirao
      Belo Horizonte
      Costa Rica
      0
      0
      England

      57823.0
      0
      0
      HAIMOUDI Djamel (ALG)
      ACHIK Redouane (MAR)
      ETCHIALI Abdelhak (ALG)
      255931
      300186484
      CRC
      ENG

      810
      2014
      24 Jun 2014 - 16:00
      Group C
      Arena Pantanal
      Cuiaba
      Japan
      1
      4
      Colombia

      40340.0
      1
      1
      PROENCA Pedro (POR)
      MIRANDA Bertino (POR)
      TRIGO Jose (POR)
      255931
      300186457
      JPN
      COL

      811
      2014
      24 Jun 2014 - 17:00
      Group C
      Estadio Castelao
      Fortaleza
      Greece
      2
      1
      C�te d'Ivoire

      59095.0
      1
      0
      VERA Carlos (ECU)
      LESCANO Christian (ECU)
      ROMERO Byron (ECU)
      255931
      300186455
      GRE
      CIV

      812
      2014
      25 Jun 2014 - 13:00
      Group F
      Estadio Beira-Rio
      Porto Alegre
      Nigeria
      2
      3
      Argentina

      43285.0
      1
      2
      Nicola RIZZOLI (ITA)
      Renato FAVERANI (ITA)
      Andrea STEFANI (ITA)
      255931
      300186458
      NGA
      ARG

      813
      2014
      25 Jun 2014 - 13:00
      Group F
      Arena Fonte Nova
      Salvador
      rn">Bosnia and Herzegovina
      3
      1
      IR Iran

      48011.0
      1
      0
      Carlos VELASCO CARBALLO (ESP)
      ALONSO FERNANDEZ Roberto (ESP)
      YUSTE Juan (ESP)
      255931
      300186464
      BIH
      IRN

      814
      2014
      25 Jun 2014 - 16:00
      Group E
      Arena Amazonia
      Manaus
      Honduras
      0
      3
      Switzerland

      40322.0
      0
      2
      PITANA Nestor (ARG)
      MAIDANA Hernan (ARG)
      BELATTI Juan Pablo (ARG)
      255931
      300186482
      HON
      SUI

      815
      2014
      25 Jun 2014 - 17:00
      Group E
      Estadio do Maracana
      Rio De Janeiro
      Ecuador
      0
      0
      France

      73749.0
      0
      0
      Noumandiez DOUE (CIV)
      YEO Songuifolo (CIV)
      BIRUMUSHAHU Jean Claude (BDI)
      255931
      300186515
      ECU
      FRA

      816
      2014
      26 Jun 2014 - 13:00
      Group G
      Arena Pernambuco
      Recife
      USA
      0
      1
      Germany

      41876.0
      0
      0
      Ravshan IRMATOV (UZB)
      RASULOV Abduxamidullo (UZB)
      KOCHKAROV Bakhadyr (KGZ)
      255931
      300186469
      USA
      GER

      817
      2014
      26 Jun 2014 - 13:00
      Group G
      Estadio Nacional
      Brasilia
      Portugal
      2
      1
      Ghana

      67540.0
      1
      0
      SHUKRALLA Nawaf (BHR)
      TULEFAT Yaser (BHR)
      SALEH Ebrahim (BHR)
      255931
      300186476
      POR
      GHA

      818
      2014
      26 Jun 2014 - 17:00
      Group H
      Arena de Sao Paulo
      Sao Paulo
      Korea Republic
      0
      1
      Belgium

      61397.0
      0
      0
      Ben WILLIAMS (AUS)
      CREAM Matthew (AUS)
      ANAZ Hakan (AUS)
      255931
      300186480
      KOR
      BEL

      819
      2014
      26 Jun 2014 - 17:00
      Group H
      Arena da Baixada
      Curitiba
      Algeria
      1
      1
      Russia

      39311.0
      0
      1
      C�neyt �AKIR (TUR)
      DURAN Bahattin (TUR)
      ONGUN Tarik (TUR)
      255931
      300186506
      ALG
      RUS

      820
      2014
      28 Jun 2014 - 13:00
      Round of 16
      Estadio Mineirao
      Belo Horizonte
      Brazil
      1
      1
      Chile
      Brazil win on penalties (3 - 2)
      57714.0
      0
      0
      WEBB Howard (ENG)
      MULLARKEY Michael (ENG)
      Darren CANN (ENG)
      255951
      300186487
      BRA
      CHI

      821
      2014
      28 Jun 2014 - 17:00
      Round of 16
      Estadio do Maracana
      Rio De Janeiro
      Colombia
      2
      0
      Uruguay

      73804.0
      1
      0
      Bj�rn KUIPERS (NED)
      Sander VAN ROEKEL (NED)
      Erwin ZEINSTRA (NED)
      255951
      300186491
      COL
      URU

      822
      2014
      30 Jun 2014 - 13:00
      Round of 16
      Estadio Nacional
      Brasilia
      France
      2
      0
      Nigeria

      67882.0
      0
      0
      GEIGER Mark (USA)
      HURD Sean (USA)
      FLETCHER Joe (CAN)
      255951
      300186462
      FRA
      NGA

      823
      2014
      30 Jun 2014 - 17:00
      Round of 16
      Estadio Beira-Rio
      Porto Alegre
      Germany
      2
      1
      Algeria
      Germany win after extra time
      NaN
      0
      0
      RICCI Sandro (BRA)
      DE CARVALHO Emerson (BRA)
      VAN GASSE Marcelo (BRA)
      255951
      300186460
      GER
      ALG

      824
      2014
      04 Jul 2014 - 17:00
      Quarter-finals
      Estadio Castelao
      Fortaleza
      Brazil
      2
      1
      Colombia

      60342.0
      1
      0
      Carlos VELASCO CARBALLO (ESP)
      ALONSO FERNANDEZ Roberto (ESP)
      YUSTE Juan (ESP)
      255953
      300186461
      BRA
      COL

      825
      2014
      04 Jul 2014 - 13:00
      Quarter-finals
      Estadio do Maracana
      Rio De Janeiro
      France
      0
      1
      Germany

      74240.0
      0
      1
      PITANA Nestor (ARG)
      MAIDANA Hernan (ARG)
      BELATTI Juan Pablo (ARG)
      255953
      300186485
      FRA
      GER

      826
      2014
      08 Jul 2014 - 17:00
      Semi-finals
      Estadio Mineirao
      Belo Horizonte
      Brazil
      1
      7
      Germany

      58141.0
      0
      5
      RODRIGUEZ Marco (MEX)
      TORRENTERA Marvin (MEX)
      QUINTERO Marcos (MEX)
      255955
      300186474
      BRA
      GER

      827
      2014
      12 Jul 2014 - 17:00
      Play-off for third place
      Estadio Nacional
      Brasilia
      Brazil
      0
      3
      Netherlands

      68034.0
      0
      2
      HAIMOUDI Djamel (ALG)
      ACHIK Redouane (MAR)
      ETCHIALI Abdelhak (ALG)
      255957
      300186502
      BRA
      NED

      828
      2014
      13 Jul 2014 - 16:00
      Final
      Estadio do Maracana
      Rio De Janeiro
      Germany
      1
      0
      Argentina
      Germany win after extra time
      74738.0
      0
      0
      Nicola RIZZOLI (ITA)
      Renato FAVERANI (ITA)
      Andrea STEFANI (ITA)
      255959
      300186501
      GER
      ARG

      829
      2014
      09 Jul 2014 - 17:00
      Semi-finals
      Arena de Sao Paulo
      Sao Paulo
      Netherlands
      0
      0
      Argentina
      Argentina win on penalties (2 - 4)
      63267.0
      0
      0
      C�neyt �AKIR (TUR)
      DURAN Bahattin (TUR)
      ONGUN Tarik (TUR)
      255955
      300186490
      NED
      ARG

      830
      2014
      05 Jul 2014 - 17:00
      Quarter-finals
      Arena Fonte Nova
      Salvador
      Netherlands
      0
      0
      Costa Rica
      Netherlands win on penalties (4 - 3)
      51179.0
      0
      0
      Ravshan IRMATOV (UZB)
      RASULOV Abduxamidullo (UZB)
      KOCHKAROV Bakhadyr (KGZ)
      255953
      300186488
      NED
      CRC

      831
      2014
      05 Jul 2014 - 13:00
      Quarter-finals
      Estadio Nacional
      Brasilia
      Argentina
      1
      0
      Belgium

      68551.0
      1
      0
      Nicola RIZZOLI (ITA)
      Renato FAVERANI (ITA)
      Andrea STEFANI (ITA)
      255953
      300186504
      ARG
      BEL

      832
      2014
      29 Jun 2014 - 13:00
      Round of 16
      Estadio Castelao
      Fortaleza
      Netherlands
      2
      1
      Mexico

      58817.0
      0
      0
      PROENCA Pedro (POR)
      MIRANDA Bertino (POR)
      TRIGO Jose (POR)
      255951
      300186508
      NED
      MEX

      833
      2014
      29 Jun 2014 - 17:00
      Round of 16
      Arena Pernambuco
      Recife
      Costa Rica
      1
      1
      Greece
      Costa Rica win on penalties (5 - 3)
      41242.0
      0
      0
      Ben WILLIAMS (AUS)
      CREAM Matthew (AUS)
      ANAZ Hakan (AUS)
      255951
      300186459
      CRC
      GRE

      834
      2014
      01 Jul 2014 - 13:00
      Round of 16
      Arena de Sao Paulo
      Sao Paulo
      Argentina
      1
      0
      Switzerland
      Argentina win after extra time
      63255.0
      0
      0
      ERIKSSON Jonas (SWE)
      KLASENIUS Mathias (SWE)
      WARNMARK Daniel (SWE)
      255951
      300186503
      ARG
      SUI

      835
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      Belgium win after extra time
      51227.0
      0
      0
      HAIMOUDI Djamel (ALG)
      ACHIK Redouane (MAR)
      ETCHIALI Abdelhak (ALG)
      255951
      300186497
      BEL
      USA

836 rows × 20 columns

```python
df_players
```

      RoundID
      MatchID
      Team Initials
      Coach Name
      Line-up
      Shirt Number
      Player Name
      Position
      Event

      0
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Alex THEPOT
      GK
      NaN

      1
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Oscar BONFIGLIO
      GK
      NaN

      2
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Marcel LANGILLER
      NaN
      G40'

      3
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Juan CARRENO
      NaN
      G70'

      4
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Ernest LIBERATI
      NaN
      NaN

      5
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Rafael GARZA
      C
      NaN

      6
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Andre MASCHINOT
      NaN
      G43' G87'

      7
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Hilario LOPEZ
      NaN
      NaN

      8
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Etienne MATTLER
      NaN
      NaN

      9
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Dionisio MEJIA
      NaN
      NaN

      10
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Marcel PINEL
      NaN
      NaN

      11
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Felipe ROSAS
      NaN
      NaN

      12
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Alex VILLAPLANE
      C
      NaN

      13
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Manuel ROSAS
      NaN
      NaN

      14
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Lucien LAURENT
      NaN
      G19'

      15
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Jose RUIZ
      NaN
      NaN

      16
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Marcel CAPELLE
      NaN
      NaN

      17
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Alfredo SANCHEZ
      NaN
      NaN

      18
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Augustin CHANTREL
      NaN
      NaN

      19
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Efrain AMEZCUA
      NaN
      NaN

      20
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      S
      0
      Edmond DELFOUR
      NaN
      NaN

      21
      201
      1096
      MEX
      LUQUE Juan (MEX)
      S
      0
      Luis PEREZ
      NaN
      NaN

      22
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      N
      0
      Celestin DELMER
      NaN
      NaN

      23
      201
      1096
      MEX
      LUQUE Juan (MEX)
      N
      0
      Isidoro SOTA
      NaN
      NaN

      24
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      N
      0
      Andre TASSIN
      NaN
      NaN

      25
      201
      1096
      MEX
      LUQUE Juan (MEX)
      N
      0
      Jesus CASTRO
      NaN
      NaN

      26
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      N
      0
      Nouma ANDOIRE
      NaN
      NaN

      27
      201
      1096
      MEX
      LUQUE Juan (MEX)
      N
      0
      Felipe OLIVARES
      NaN
      NaN

      28
      201
      1096
      FRA
      CAUDRON Raoul (FRA)
      N
      0
      Jean LAURENT
      NaN
      NaN

      29
      201
      1096
      MEX
      LUQUE Juan (MEX)
      N
      0
      Raimundo RODRIGUEZ
      NaN
      NaN

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      37754
      255959
      300186501
      GER
      LOEW Joachim (GER)
      S
      18
      KROOS
      NaN
      NaN

      37755
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      S
      15
      DEMICHELIS
      NaN
      NaN

      37756
      255959
      300186501
      GER
      LOEW Joachim (GER)
      S
      20
      BOATENG
      NaN
      NaN

      37757
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      S
      16
      ROJO
      NaN
      NaN

      37758
      255959
      300186501
      GER
      LOEW Joachim (GER)
      S
      23
      KRAMER
      NaN
      O31'

      37759
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      S
      22
      LAVEZZI
      NaN
      OH46'

      37760
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      12
      ZIELER
      GK
      NaN

      37761
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      12
      ORION
      GK
      NaN

      37762
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      22
      WEIDENFELLER
      GK
      NaN

      37763
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      21
      ANDUJAR
      GK
      NaN

      37764
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      2
      GRO�KREUTZ
      NaN
      NaN

      37765
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      3
      CAMPAGNARO
      NaN
      NaN

      37766
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      3
      GINTER
      NaN
      NaN

      37767
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      5
      GAGO
      NaN
      I86'

      37768
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      9
      SCH�RRLE
      NaN
      I31'

      37769
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      7
      DI MARIA
      NaN
      NaN

      37770
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      10
      PODOLSKI
      NaN
      NaN

      37771
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      11
      M. RODRIGUEZ
      NaN
      NaN

      37772
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      14
      DRAXLER
      NaN
      NaN

      37773
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      13
      A. FERNANDEZ
      NaN
      NaN

      37774
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      15
      DURM
      NaN
      NaN

      37775
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      17
      F. FERNANDEZ
      NaN
      NaN

      37776
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      17
      MERTESACKER
      NaN
      I120'

      37777
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      18
      PALACIO
      NaN
      I78'

      37778
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      19
      G�TZE
      NaN
      I88' G113'

      37779
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      19
      ALVAREZ
      NaN
      NaN

      37780
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      6
      KHEDIRA
      NaN
      NaN

      37781
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      20
      AGUERO
      NaN
      IH46' Y65'

      37782
      255959
      300186501
      GER
      LOEW Joachim (GER)
      N
      21
      MUSTAFI
      NaN
      NaN

      37783
      255959
      300186501
      ARG
      SABELLA Alejandro (ARG)
      N
      23
      BASANTA
      NaN
      NaN

37784 rows × 9 columns

```python
df_events
```

      Year
      Datetime
      Stage
      Stadium
      City
      Home Team Name
      Home Team Goals
      Away Team Goals
      Away Team Name
      Attendance
      ...
      GoldenGoal
      DecisionPenaltyAway
      DecisionPenaltyHome
      HomeTeamWins
      AwayTeamWins
      EventOfHomeTeam
      EventOfWinner
      EventOfLoser
      HourGameStart
      StageRank

      0
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      1
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      2
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      3
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      4
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      5
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      6
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      7
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      8
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      9
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      10
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      11
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      12
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      13
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      14
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      15
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      16
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      17
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      18
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      19
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      20
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      21
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      22
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      23
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      24
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      25
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      26
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      27
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      28
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      15
      1

      29
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      15
      1

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      39389
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39390
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39391
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39392
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39393
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39394
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39395
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39396
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39397
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39398
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39399
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39400
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39401
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39402
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39403
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39404
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39405
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39406
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39407
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39408
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39409
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39410
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39411
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39412
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39413
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39414
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39415
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39416
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

      39417
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      True
      True
      False
      17
      2

      39418
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227
      ...
      False
      0
      0
      True
      False
      False
      False
      True
      17
      2

39419 rows × 39 columns

# Descriptive part
In this section we provide some general details about the dataset. First question is: Who won most world cups? Who won most second prizes?

```python
plt.figure(figsize=(14,7))
plt.subplot(121)
df_cups.groupby(['Winner']).size().sort_values(ascending=False).plot.bar()
plt.title("World Cup Wins")
plt.xlabel("")

plt.subplot(122)
df_cups.groupby(['Runners-Up']).size().sort_values(ascending=False).plot.bar()
plt.title("2nd Prize")
plt.xlabel("")

plt.show()
```

Next question is: Who scored most goals in world championships?

```python
plt.figure(figsize=(9,6))

df_players.groupby(['Player Name']).size().sort_values(ascending=False).nlargest(n=10).plot.bar()
print(df_players.groupby(['Player Name']).size().sort_values(ascending=False).nlargest(n=10))
```

```python
# data consistency not sufficient for this calculation as player names are not unique
# note that this does not have an impact on the subsequent analysis

#df_timespan = df_joined_matches[['Player Name','Year']].groupby(['Player Name']).aggregate(['min','max']).Year
#df_timespan.columns = ['start','end']
#df_timespan.apply(lambda x: (x.end-x.start), axis=1).sort_values(ascending = False)
```

# Analysis of Fair Play

We first need some preliminary work. How many matches were played at all? How many of them were won by one team?

```python
num_matches_total = len(df_events.groupby('MatchID').mean())
num_matches_decision = len(df_events.loc[(df_events['HomeTeamWins'] == True) | (df_events['AwayTeamWins'] == True)].groupby('MatchID').mean())
num_matches_tie = len(df_events.loc[(df_events['HomeTeamWins'] == False) & (df_events['AwayTeamWins'] == False)].groupby('MatchID').mean())

print("num_matches_total: %g"% num_matches_total)
print("num_matches_decision: %g"% num_matches_decision)
print("num_matches_tie: %g"% num_matches_tie)

print("proportion decision: %.2f"% (num_matches_decision/num_matches_total*100))
print("proportion no decision: %.2f"% (num_matches_tie/num_matches_total*100))
```

    num_matches_total: 836
    num_matches_decision: 676
    num_matches_tie: 160
    proportion decision: 80.86
    proportion no decision: 19.14

## Yellow and Red Cards Statistics

On average 2.68 yellow cards, 0.14 red cards and 0.06 red cards for second yellow are given during a match.

```python
f = {
     'Year':'count' # we could do this with any attribute
    }

df_cards = df_events.loc[(df_events["EventType"] == "Y") | (df_events["EventType"] == "R") | (df_events["EventType"] == "RSY")]
df_cards = df_cards.groupby(["EventType"]).agg(f)
df_cards.columns = ['Total']
df_cards.assign(AvgPerMatch = lambda x : x.Total/num_matches_total)
```

      Total
      AvgPerMatch

      EventType

      R
      117
      0.139952

      RSY
      52
      0.062201

      Y
      2246
      2.686603

The match with most cards was Portugal vs. Greece in 2006's Round of 16 with 20 cards.

```python
f = {
     'Attendance':'count' # again, we could do this with any attribute
    }

df_cards = df_events.loc[(df_events["EventType"] == "Y") | (df_events["EventType"] == "R") | (df_events["EventType"] == "RSY")]
df_cards = df_cards.groupby(['MatchID','Stage','Year','Home Team Name', 'Away Team Name', 'Home Team Goals', 'Away Team Goals']).agg(f).reset_index()
df_cards.columns = ['Match ID', 'Stage','Year', 'Home Team Name', 'Away Team Name', 'Home Team Goals', 'Away Team Goals', 'Cards']
df_cards.sort_values(by=['Cards'], ascending=False)
```

      Match ID
      Stage
      Year
      Home Team Name
      Away Team Name
      Home Team Goals
      Away Team Goals
      Cards

      474
      97410052
      Round of 16
      2006
      Portugal
      Netherlands
      1
      0
      20

      556
      300186459
      Round of 16
      2014
      Costa Rica
      Greece
      1
      1
      18

      394
      43950035
      Group E
      2002
      Cameroon
      Germany
      0
      2
      18

      540
      300061509
      Final
      2010
      Netherlands
      Spain
      0
      1
      15

      584
      300186487
      Round of 16
      2014
      Brazil
      Chile
      1
      1
      14

      288
      3092
      Round of 16
      1994
      Mexico
      Bulgaria
      1
      1
      12

      393
      43950034
      Group A
      2002
      Senegal
      Uruguay
      3
      3
      12

      585
      300186488
      Quarter-finals
      2014
      Netherlands
      Costa Rica
      0
      0
      12

      466
      97410044
      Group F
      2006
      Croatia
      Australia
      2
      2
      11

      411
      43950053
      Round of 16
      2002
      Mexico
      USA
      0
      2
      11

      460
      97410038
      Group C
      2006
      C�te d'Ivoire
      rn">Serbia and Montenegro
      3
      2
      11

      456
      97410034
      Group A
      2006
      Costa Rica
      Poland
      1
      2
      10

      599
      300186502
      Play-off for third place
      2014
      Brazil
      Netherlands
      0
      3
      10

      600
      300186503
      Round of 16
      2014
      Argentina
      Switzerland
      1
      0
      10

      287
      3091
      Round of 16
      1994
      Nigeria
      Italy
      1
      2
      10

      461
      97410039
      Group D
      2006
      Portugal
      Mexico
      2
      1
      10

      414
      43950056
      Round of 16
      2002
      Korea Republic
      Italy
      2
      1
      10

      318
      8746
      Group C
      1998
      South Africa
      Denmark
      1
      1
      10

      84
      575
      Quarter-finals
      1986
      Germany FR
      Mexico
      0
      0
      10

      280
      3084
      Group D
      1994
      Argentina
      Bulgaria
      0
      2
      10

      526
      300061493
      Group H
      2010
      Chile
      Switzerland
      1
      0
      10

      9
      48
      Group A
      1990
      Austria
      USA
      2
      1
      10

      507
      300061470
      Group D
      2010
      Germany
      Serbia
      0
      1
      10

      468
      97410046
      Group G
      2006
      Switzerland
      Korea Republic
      2
      0
      10

      405
      43950047
      Group D
      2002
      Portugal
      Korea Republic
      0
      1
      9

      286
      3090
      Round of 16
      1994
      Brazil
      USA
      1
      0
      9

      59
      427
      Group B
      1986
      Iraq
      Belgium
      1
      2
      9

      470
      97410048
      Group H
      2006
      Ukraine
      Tunisia
      1
      0
      9

      477
      97410055
      Round of 16
      2006
      Brazil
      Ghana
      3
      0
      9

      290
      3096
      Quarter-finals
      1994
      Bulgaria
      Germany
      2
      1
      8

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      146
      1422
      Group 1
      1958
      Northern Ireland
      Czechoslovakia
      2
      1
      1

      113
      833
      Group 1
      1982
      Peru
      Cameroon
      0
      0
      1

      180
      1884
      Group 2
      1970
      Uruguay
      Italy
      0
      0
      1

      226
      2246
      Group 3
      1978
      Brazil
      Spain
      0
      0
      1

      205
      2065
      Group B
      1974
      Germany FR
      Sweden
      4
      2
      1

      68
      463
      Round of 16
      1986
      Mexico
      Bulgaria
      2
      0
      1

      222
      2200
      Group 1
      1978
      Italy
      Argentina
      1
      0
      1

      72
      517
      Group E
      1986
      Scotland
      Denmark
      0
      1
      1

      548
      300111117
      Group E
      2010
      Netherlands
      Japan
      1
      0
      1

      219
      2197
      Group 1
      1978
      Argentina
      France
      2
      1
      1

      76
      537
      Group F
      1986
      England
      Poland
      3
      0
      1

      476
      97410054
      Round of 16
      2006
      Switzerland
      Ukraine
      0
      0
      1

      215
      2181
      Group 3
      1974
      Sweden
      Uruguay
      3
      0
      1

      423
      97410001
      Group A
      2006
      Germany
      Costa Rica
      4
      2
      1

      208
      2085
      Group 4
      1974
      Haiti
      Poland
      0
      7
      1

      207
      2083
      Group 4
      1974
      Italy
      Haiti
      3
      1
      1

      90
      674
      Group F
      1986
      Morocco
      Poland
      0
      0
      1

      183
      1925
      Quarter-finals
      1970
      Uruguay
      Soviet Union
      1
      0
      1

      91
      675
      Group F
      1986
      Portugal
      Morocco
      1
      3
      1

      533
      300061502
      Round of 16
      2010
      Argentina
      Mexico
      3
      1
      1

      95
      739
      Group 2
      1982
      Algeria
      Austria
      0
      2
      1

      96
      740
      Group 2
      1982
      Algeria
      Chile
      3
      2
      1

      191
      1954
      Group 1
      1974
      Australia
      Germany FR
      0
      3
      1

      103
      767
      Group 4
      1982
      Austria
      France
      0
      1
      1

      523
      300061490
      Group G
      2010
      Brazil
      Korea DPR
      2
      1
      1

      105
      771
      Group 4
      1982
      Austria
      Northern Ireland
      2
      2
      1

      184
      1945
      Group A
      1974
      Argentina
      Brazil
      1
      2
      1

      108
      782
      Group 1
      1982
      Poland
      Belgium
      3
      0
      1

      109
      783
      Group 1
      1982
      Belgium
      Soviet Union
      0
      1
      1

      141
      1165
      First round
      1938
      Switzerland
      Germany
      1
      1
      1

613 rows × 8 columns

During the match, 4 players were given a red card by second yellow.

```python
df_events.loc[(df_events["MatchID"] == 97410052.0) & (df_events["EventType"] != "") & (df_events["EventType"] != "I")][['Team Initials','Player Name','EventMinute','EventType']]

#[df_events.MatchID == 97410052][['EventOfHomeTeam','EventType','Player Name']]
```

      Team Initials
      Player Name
      EventMinute
      EventType

      31789
      POR
      RICARDO
      76
      Y

      31792
      NED
      BOULAHROUZ
      7
      Y

      31793
      NED
      BOULAHROUZ
      63
      Y

      31794
      NED
      BOULAHROUZ
      63
      RSY

      31795
      POR
      COSTINHA
      31
      Y

      31796
      POR
      COSTINHA
      45
      Y

      31797
      POR
      COSTINHA
      45
      RSY

      31798
      NED
      MATHIJSEN
      56
      O

      31799
      POR
      FIGO
      60
      Y

      31800
      POR
      FIGO
      84
      O

      31801
      NED
      V. BRONCKHORST
      59
      Y

      31802
      NED
      V. BRONCKHORST
      90
      Y

      31803
      NED
      V. BRONCKHORST
      90
      RSY

      31804
      POR
      PAULETA
      46
      O

      31807
      NED
      COCU
      84
      O

      31808
      POR
      N.VALENTE
      76
      Y

      31812
      POR
      C.RONALDO
      34
      O

      31814
      POR
      MANICHE
      20
      Y

      31815
      POR
      MANICHE
      23
      G

      31816
      NED
      V. BOMMEL
      2
      Y

      31817
      NED
      V. BOMMEL
      67
      O

      31818
      POR
      DECO
      73
      Y

      31819
      POR
      DECO
      78
      Y

      31820
      POR
      DECO
      78
      RSY

      31821
      NED
      SNEIJDER
      73
      Y

      31829
      POR
      PETIT
      50
      Y

      31831
      NED
      VAN DER VAART
      74
      Y

## Event minutes of red and yellow cards

In this section we want to find out when most yellow and red cards are given. As expected, red cards tend to be given later.

```python
df_events[['EventMinute']] = df_events[['EventMinute']].apply(pd.to_numeric)
#df_events.loc[(df_events['EventType'] == "Y") & (int(df_events['EventMinute']) < 20)]
minutes_yellow = df_events[df_events.EventType == "Y"].EventMinute.values
minutes_red = df_events[df_events.EventType == "R"].EventMinute.values
minutes_red_2nd_yellow = df_events[df_events.EventType == "RSY"].EventMinute.values

plt.figure(figsize=(12,6))

ax = plt.subplot(131)

ax.boxplot(minutes_yellow)
plt.title("Yellow Cards")

ax = plt.subplot(132)

ax.boxplot(minutes_red)
plt.title("Red Cards")

ax = plt.subplot(133)

ax.boxplot(minutes_red_2nd_yellow)
plt.title("Red Cards (By 2nd Yellow)")

plt.show()
```

# Fairest team

In this section we want to find out which team is given the fewest yellow cards per match on average. First we have to find out how many yellow cards a team was awarded. We have to create to dataframes as teams appear either as home or away team.

```python
df_yellow_cards = df_events[df_events.EventType == "Y"]

df_yellow_cards_home = df_yellow_cards[df_yellow_cards.EventOfHomeTeam == True][["Home Team Name", "EventType"]]
df_yellow_cards_home = df_yellow_cards_home.groupby("Home Team Name").count().reset_index()
df_yellow_cards_home.columns = ['Team', 'YellowCardsHome']

df_yellow_cards_away = df_yellow_cards[df_yellow_cards.EventOfHomeTeam == False][["Away Team Name", "EventType"]]
df_yellow_cards_away = df_yellow_cards_away.groupby("Away Team Name").count().reset_index()
df_yellow_cards_away.columns = ['Team', 'YellowCardsAway']

df_yellow_cards_count = pd.merge(df_yellow_cards_home, df_yellow_cards_away).fillna(0)
df_yellow_cards_count['YellowCardsTotal'] = df_yellow_cards_count.YellowCardsHome+df_yellow_cards_count.YellowCardsAway
df_yellow_cards_count
```

      Team
      YellowCardsHome
      YellowCardsAway
      YellowCardsTotal

      0
      Algeria
      9
      10
      19

      1
      Angola
      3
      8
      11

      2
      Argentina
      70
      50
      120

      3
      Australia
      12
      14
      26

      4
      Austria
      12
      11
      23

      5
      Belgium
      18
      23
      41

      6
      Brazil
      86
      23
      109

      7
      Bulgaria
      13
      26
      39

      8
      Cameroon
      26
      22
      48

      9
      Chile
      22
      26
      48

      10
      China PR
      3
      2
      5

      11
      Colombia
      10
      15
      25

      12
      Costa Rica
      20
      22
      42

      13
      Croatia
      7
      30
      37

      14
      Czech Republic
      3
      4
      7

      15
      Czechoslovakia
      3
      15
      18

      16
      C�te d'Ivoire
      8
      13
      21

      17
      Denmark
      13
      18
      31

      18
      Ecuador
      6
      17
      23

      19
      England
      30
      21
      51

      20
      France
      28
      31
      59

      21
      German DR
      6
      4
      10

      22
      Germany
      55
      24
      79

      23
      Germany FR
      26
      14
      40

      24
      Ghana
      10
      25
      35

      25
      Greece
      6
      17
      23

      26
      Haiti
      1
      2
      3

      27
      Honduras
      5
      10
      15

      28
      Hungary
      2
      4
      6

      29
      IR Iran
      1
      9
      10

      ...
      ...
      ...
      ...
      ...

      42
      Norway
      2
      11
      13

      43
      Paraguay
      16
      25
      41

      44
      Peru
      1
      8
      9

      45
      Poland
      20
      22
      42

      46
      Portugal
      34
      12
      46

      47
      Romania
      20
      12
      32

      48
      Russia
      6
      16
      22

      49
      Saudi Arabia
      12
      10
      22

      50
      Scotland
      8
      9
      17

      51
      Senegal
      9
      5
      14

      52
      Serbia
      4
      6
      10

      53
      Slovakia
      7
      4
      11

      54
      Slovenia
      11
      8
      19

      55
      South Africa
      16
      5
      21

      56
      Soviet Union
      4
      14
      18

      57
      Spain
      33
      31
      64

      58
      Sweden
      19
      23
      42

      59
      Switzerland
      10
      23
      33

      60
      Togo
      6
      4
      10

      61
      Tunisia
      10
      20
      30

      62
      Turkey
      3
      16
      19

      63
      USA
      19
      34
      53

      64
      Ukraine
      4
      8
      12

      65
      Uruguay
      23
      41
      64

      66
      Yugoslavia
      10
      18
      28

      67
      rn">Bosnia and Herzegovina
      1
      2
      3

      68
      rn">Republic of Ireland
      9
      7
      16

      69
      rn">Serbia and Montenegro
      4
      8
      12

      70
      rn">Trinidad and Tobago
      3
      7
      10

      71
      rn">United Arab Emirates
      3
      4
      7

72 rows × 4 columns

Now we need the amount of matches per team to finally compute the average amount of yellow cards per match per team.

```python
df_home_matches = df_matches[["Home Team Name"]]
df_home_matches["MatchesCount1"] = 1
df_home_matches = df_home_matches.groupby("Home Team Name").count().reset_index()
df_home_matches.columns = ['Team', 'MatchesHome']

df_away_matches = df_matches[["Away Team Name"]]
df_away_matches["MatchesCount2"] = 1
df_away_matches = df_away_matches.groupby("Away Team Name").count().reset_index()
df_away_matches.columns = ['Team', 'MatchesAway']

df_matches_count = pd.merge(df_home_matches, df_away_matches).fillna(0)
df_matches_count['MatchesTotal'] = df_matches_count.MatchesHome+df_matches_count.MatchesAway

df_yellow_cards_teams = pd.merge(df_yellow_cards_count, df_matches_count)
df_yellow_cards_teams['AvgYellowPerMatch'] = df_yellow_cards_teams.YellowCardsTotal/df_yellow_cards_teams.MatchesTotal
#just to get team as index
df_yellow_cards_teams = df_yellow_cards_teams.groupby("Team").mean()
df_yellow_cards_teams
```

    C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: SettingWithCopyWarning:
    A value is trying to be set on a copy of a slice from a DataFrame.
    Try using .loc[row_indexer,col_indexer] = value instead

    See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

    C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:7: SettingWithCopyWarning:
    A value is trying to be set on a copy of a slice from a DataFrame.
    Try using .loc[row_indexer,col_indexer] = value instead

    See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
      import sys

      YellowCardsHome
      YellowCardsAway
      YellowCardsTotal
      MatchesHome
      MatchesAway
      MatchesTotal
      AvgYellowPerMatch

      Team

      Algeria
      9
      10
      19
      6
      7
      13
      1.461538

      Angola
      3
      8
      11
      1
      2
      3
      3.666667

      Argentina
      70
      50
      120
      52
      25
      77
      1.558442

      Australia
      12
      14
      26
      6
      7
      13
      2.000000

      Austria
      12
      11
      23
      13
      16
      29
      0.793103

      Belgium
      18
      23
      41
      17
      24
      41
      1.000000

      Brazil
      86
      23
      109
      78
      26
      104
      1.048077

      Bulgaria
      13
      26
      39
      7
      19
      26
      1.500000

      Cameroon
      26
      22
      48
      11
      12
      23
      2.086957

      Chile
      22
      26
      48
      14
      19
      33
      1.454545

      China PR
      3
      2
      5
      1
      2
      3
      1.666667

      Colombia
      10
      15
      25
      6
      12
      18
      1.388889

      Costa Rica
      20
      22
      42
      6
      9
      15
      2.800000

      Croatia
      7
      30
      37
      3
      13
      16
      2.312500

      Czech Republic
      3
      4
      7
      2
      1
      3
      2.333333

      Czechoslovakia
      3
      15
      18
      10
      20
      30
      0.600000

      C�te d'Ivoire
      8
      13
      21
      3
      6
      9
      2.333333

      Denmark
      13
      18
      31
      7
      9
      16
      1.937500

      Ecuador
      6
      17
      23
      4
      6
      10
      2.300000

      England
      30
      21
      51
      35
      27
      62
      0.822581

      France
      28
      31
      59
      29
      30
      59
      1.000000

      German DR
      6
      4
      10
      3
      3
      6
      1.666667

      Germany
      55
      24
      79
      32
      12
      44
      1.795455

      Germany FR
      26
      14
      40
      43
      19
      62
      0.645161

      Ghana
      10
      25
      35
      4
      8
      12
      2.916667

      Greece
      6
      17
      23
      4
      6
      10
      2.300000

      Haiti
      1
      2
      3
      1
      2
      3
      1.000000

      Honduras
      5
      10
      15
      5
      4
      9
      1.666667

      Hungary
      2
      4
      6
      18
      14
      32
      0.187500

      IR Iran
      1
      9
      10
      1
      5
      6
      1.666667

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      Norway
      2
      11
      13
      1
      7
      8
      1.625000

      Paraguay
      16
      25
      41
      11
      16
      27
      1.518519

      Peru
      1
      8
      9
      5
      10
      15
      0.600000

      Poland
      20
      22
      42
      16
      15
      31
      1.354839

      Portugal
      34
      12
      46
      16
      10
      26
      1.769231

      Romania
      20
      12
      32
      9
      12
      21
      1.523810

      Russia
      6
      16
      22
      3
      6
      9
      2.444444

      Saudi Arabia
      12
      10
      22
      6
      7
      13
      1.692308

      Scotland
      8
      9
      17
      9
      14
      23
      0.739130

      Senegal
      9
      5
      14
      2
      3
      5
      2.800000

      Serbia
      4
      6
      10
      1
      2
      3
      3.333333

      Slovakia
      7
      4
      11
      2
      2
      4
      2.750000

      Slovenia
      11
      8
      19
      3
      3
      6
      3.166667

      South Africa
      16
      5
      21
      6
      3
      9
      2.333333

      Soviet Union
      4
      14
      18
      18
      13
      31
      0.580645

      Spain
      33
      31
      64
      30
      29
      59
      1.084746

      Sweden
      19
      23
      42
      27
      19
      46
      0.913043

      Switzerland
      10
      23
      33
      12
      21
      33
      1.000000

      Togo
      6
      4
      10
      2
      1
      3
      3.333333

      Tunisia
      10
      20
      30
      4
      8
      12
      2.500000

      Turkey
      3
      16
      19
      2
      8
      10
      1.900000

      USA
      19
      34
      53
      15
      18
      33
      1.606061

      Ukraine
      4
      8
      12
      1
      4
      5
      2.400000

      Uruguay
      23
      41
      64
      28
      23
      51
      1.254902

      Yugoslavia
      10
      18
      28
      17
      20
      37
      0.756757

      rn">Bosnia and Herzegovina
      1
      2
      3
      1
      2
      3
      1.000000

      rn">Republic of Ireland
      9
      7
      16
      5
      8
      13
      1.230769

      rn">Serbia and Montenegro
      4
      8
      12
      1
      2
      3
      4.000000

      rn">Trinidad and Tobago
      3
      7
      10
      1
      2
      3
      3.333333

      rn">United Arab Emirates
      3
      4
      7
      1
      2
      3
      2.333333

72 rows × 7 columns

We see that some teams with only very few matches appear in both lists. These could be statistical outliers.

```python
plt.figure(figsize=(12,6))

ax = plt.subplot(121)
df_yellow_cards_teams["AvgYellowPerMatch"].sort_values(ascending=False).nlargest(n=10).plot.bar()
plt.title("Teams with most yellow cards")

ax = plt.subplot(122)
df_yellow_cards_teams["AvgYellowPerMatch"].sort_values(ascending=True).nsmallest(n=10).plot.bar()
plt.title("Fairest Teams")

plt.show()
```

Based on the previous observation we restrict ourselves to teams with at least 30 matches which are in total 21 teams. Germany is on rank 3.

```python
df_yellow_cards_reg_teams = df_yellow_cards_teams[df_yellow_cards_teams.MatchesTotal > 30]
print(len(df_yellow_cards_reg_teams))

plt.figure(figsize=(12,6))

df_yellow_cards_reg_teams["AvgYellowPerMatch"].sort_values(ascending=False).plot.bar()
plt.title("Teams with most yellow cards")

plt.show()
```

## Winners play fair!?

Whether or not a game is a tie does not have an effect on the amount of red or yellow cards. However, if it is not a tie the winner are given less yellow and red cards.

```python
avg_yellow_of_winner = len(df_events.loc[(df_events['EventOfWinner'] == True) & (df_events['EventType'] == 'Y')])/num_matches_decision
avg_yellow_of_loser = len(df_events.loc[(df_events['EventOfLoser'] == True) & (df_events['EventType'] == 'Y')])/num_matches_decision

avg_red_of_winner = len(df_events.loc[(df_events['EventOfWinner'] == True) & ((df_events['EventType'] == 'R') | (df_events['EventType'] == 'RSY'))])/num_matches_decision
avg_red_of_loser = len(df_events.loc[(df_events['EventOfLoser'] == True) & ((df_events['EventType'] == 'R') | (df_events['EventType'] == 'RSY'))])/num_matches_decision

avg_yellow_decided_match = avg_yellow_of_winner+avg_yellow_of_loser
avg_red_decided_match = avg_red_of_winner+avg_red_of_loser

avg_yellow_tie_match = len(df_events.loc[(df_events['HomeTeamWins'] == False) & (df_events['AwayTeamWins'] == False) & (df_events['EventType'] == 'Y')])/num_matches_tie
avg_red_tie_match = len(df_events.loc[(df_events['HomeTeamWins'] == False) & (df_events['AwayTeamWins'] == False) & ((df_events['EventType'] == 'R') | (df_events['EventType'] == 'RSY'))])/num_matches_tie

print("avg_yellow_of_winner: %.2f"% (avg_yellow_of_winner))
print("avg_yellow_of_loser: %.2f"% (avg_yellow_of_loser))
print("avg_yellow_decided_match: %.2f"% (avg_yellow_decided_match))
print("avg_yellow_tie_match: %.2f"% (avg_yellow_tie_match))

print("avg_red_of_winner: %.2f"% (avg_red_of_winner))
print("avg_red_of_loser: %.2f"% (avg_red_of_loser))
print("avg_red_decided_match: %.2f"% (avg_red_decided_match))
print("avg_red_tie_match: %.2f"% (avg_red_tie_match))

plt.figure(figsize=(12,6))

ind = np.arange(2)
width = 0.35
dist = 0.2

ax = plt.subplot(121)

yellow_cards = (avg_yellow_of_winner, avg_yellow_of_loser)
red_cards = (avg_red_of_winner, avg_red_of_loser)
plt.xticks(ind, ('Winner', 'Loser'))
ax.bar(ind, yellow_cards, width, color='y')
ax.bar(ind + width, red_cards, width, color='r')

ax = plt.subplot(122)

yellow_cards = (avg_yellow_decided_match, avg_yellow_tie_match)
red_cards = (avg_red_decided_match, avg_red_tie_match)
plt.xticks(ind, ('Decided', 'Tie'))
ax.bar(ind, yellow_cards, width, color='y')
ax.bar(ind + width, red_cards, width, color='r')

plt.show()
```

# Predict Yellow Cards
In this section we trained a model to predict the amount of yellow cards given in a match.

## Initial Features for Regression
- Hour game starts, just added for fun. We do not expect a correlation.
- The year the match took place
- The stage (group phase, quarter-finals etc.) We chose to perform a one hot encoding.
- The total amount of goals
- The goal difference
- The goal difference in the half time
- The change of these differences in the second part of the match
- Whether there was extra time
- Whether penalty decided the match
- The amount of substitutions
- The amount of substitutions at half time

## Explanation of Event Types
The codes for the event types are: G=Goal, OG=Own Goal, Y=Yellow Card, R=Red Card, SY = Red Card by second yellow, P=Penalty, MP=Missed Penalty, I = Substitution In, O=Substitute Out, IH= In half time?

First, we need some data preparation. Specifically, we build the columns:
- One Hot Encoding for the EventType column (needed for groupby later on)
- One Hot Encoding for the StageRank column (for the actual regression)
- total goals scored
- goal difference half time
- goal difference end
- delta of the last two values

Additionally, attendance must be a numeric data type

```python
df_events_ohe = pd.concat([df_events, pd.get_dummies(df_events['EventType'])], axis=1)
df_events_ohe = pd.concat([df_events_ohe, pd.get_dummies(df_events['StageRank'], prefix="Stage")], axis=1)

df_events_ohe = df_events_ohe.assign(GoalsTotal = lambda x : x['Home Team Goals']+x['Away Team Goals'])
df_events_ohe = df_events_ohe.assign(GoalDifference = lambda x : abs(x['Home Team Goals']-x['Away Team Goals']))
df_events_ohe = df_events_ohe.assign(GoalDifferenceHalfTime = lambda x : abs(x['Half-time Home Goals']-x['Half-time Away Goals']))
df_events_ohe = df_events_ohe.assign(DeltaGoals = lambda x : x['GoalDifference']-x['GoalDifferenceHalfTime'])
df_events_ohe[['Attendance']] = df_events_ohe[['Attendance']].apply(pd.to_numeric)
df_events_ohe
```

      Year
      Datetime
      Stage
      Stadium
      City
      Home Team Name
      Home Team Goals
      Away Team Goals
      Away Team Name
      Attendance
      ...
      Stage_1
      Stage_2
      Stage_3
      Stage_4
      Stage_5
      Stage_6
      GoalsTotal
      GoalDifference
      GoalDifferenceHalfTime
      DeltaGoals

      0
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      1
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      2
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      3
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      4
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      5
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      6
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      7
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      8
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      9
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      10
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      11
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      12
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      13
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      14
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      15
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      16
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      17
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      18
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      19
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      20
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      21
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      22
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      23
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      24
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      25
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      26
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      27
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      28
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      29
      1930
      13 Jul 1930 - 15:00
      Group 1
      Pocitos
      Montevideo
      France
      4
      1
      Mexico
      4444.0
      ...
      1
      0
      0
      0
      0
      0
      5
      3
      3
      0

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      39389
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39390
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39391
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39392
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39393
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39394
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39395
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39396
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39397
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39398
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39399
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39400
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39401
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39402
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39403
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39404
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39405
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39406
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39407
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39408
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39409
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39410
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39411
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39412
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39413
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39414
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39415
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39416
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39417
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

      39418
      2014
      01 Jul 2014 - 17:00
      Round of 16
      Arena Fonte Nova
      Salvador
      Belgium
      2
      1
      USA
      51227.0
      ...
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1

39419 rows × 62 columns

Perform a group by to get sum of yellow cards

```python
f = {'HourGameStart':['mean'],
     #'Home Team Goals':['mean'], # not symmetric -> throw out
     #'Away Team Goals':['mean'],
     #'Half-time Home Goals':['mean'],
     #'Half-time Away Goals':['mean'],
     'Year':['mean'],
     'Stage_1':['mean'],
     'Stage_2':['mean'],
     'Stage_3':['mean'],
     'Stage_4':['mean'],
     'Stage_5':['mean'],
     'Stage_6':['mean'],
     'GoalsTotal':['mean'],
     'GoalDifference':['mean'],
     'GoalDifferenceHalfTime':['mean'],
     'DeltaGoals':['mean'],
     'ExtraTime':['mean'],
     'Penalty':['mean'],
     'I':['sum'], #substitutions
     'IH':['sum'], #substitutions half time
     'Y':['sum'],
    }

df_events_grp = df_events_ohe.groupby(['MatchID']).agg(f)
df_events_grp.columns = df_events_grp.columns.get_level_values(0)
df_events_grp
```

      HourGameStart
      Year
      Stage_1
      Stage_2
      Stage_3
      Stage_4
      Stage_5
      Stage_6
      GoalsTotal
      GoalDifference
      GoalDifferenceHalfTime
      DeltaGoals
      ExtraTime
      Penalty
      I
      IH
      Y

      MatchID

      25
      17
      1990
      0
      1
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      3
      0
      5

      26
      18
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      3
      1
      4

      27
      20
      1990
      0
      0
      0
      0
      0
      1
      1
      1
      0
      1
      False
      False
      2
      1
      4

      28
      20
      1990
      0
      0
      0
      1
      0
      0
      2
      0
      0
      0
      True
      True
      3
      1
      7

      29
      21
      1990
      1
      0
      0
      0
      0
      0
      2
      0
      0
      0
      False
      False
      4
      0
      5

      30
      21
      1990
      1
      0
      0
      0
      0
      0
      2
      2
      1
      1
      False
      False
      4
      0
      6

      31
      17
      1990
      0
      0
      1
      0
      0
      0
      0
      0
      0
      0
      True
      True
      3
      0
      5

      42
      21
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      3
      1
      1

      43
      17
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      1
      0
      False
      False
      2
      2
      5

      48
      21
      1990
      1
      0
      0
      0
      0
      0
      3
      1
      0
      1
      False
      False
      3
      1
      9

      55
      21
      1990
      0
      1
      0
      0
      0
      0
      1
      1
      0
      1
      True
      False
      4
      0
      1

      56
      17
      1990
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      4
      0
      0

      57
      17
      1990
      1
      0
      0
      0
      0
      0
      2
      2
      0
      2
      False
      False
      1
      2
      1

      66
      21
      1990
      1
      0
      0
      0
      0
      0
      4
      2
      2
      0
      False
      False
      2
      2
      3

      73
      17
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      1
      0
      False
      False
      4
      0
      4

      74
      21
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      3
      0
      2

      75
      21
      1990
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      2
      1
      4

      102
      17
      1990
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1
      True
      False
      4
      0
      6

      103
      21
      1990
      0
      0
      1
      0
      0
      0
      5
      1
      0
      1
      True
      False
      3
      1
      4

      108
      17
      1990
      1
      0
      0
      0
      0
      0
      3
      1
      0
      1
      False
      False
      4
      0
      3

      111
      21
      1990
      1
      0
      0
      0
      0
      0
      4
      4
      2
      2
      False
      False
      3
      1
      3

      114
      17
      1990
      1
      0
      0
      0
      0
      0
      2
      0
      0
      0
      False
      False
      2
      0
      4

      119
      17
      1990
      1
      0
      0
      0
      0
      0
      2
      2
      0
      2
      False
      False
      3
      0
      3

      120
      17
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      3
      1
      1

      127
      17
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      2
      1
      0

      128
      21
      1990
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      4
      0
      4

      129
      21
      1990
      0
      1
      0
      0
      0
      0
      5
      3
      1
      2
      False
      False
      1
      1
      5

      150
      21
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      4
      0
      3

      151
      21
      1990
      1
      0
      0
      0
      0
      0
      2
      0
      0
      0
      False
      False
      3
      1
      2

      152
      17
      1990
      1
      0
      0
      0
      0
      0
      0
      0
      0
      0
      False
      False
      4
      0
      2

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      300186486
      16
      2014
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      6
      0
      2

      300186487
      13
      2014
      0
      1
      0
      0
      0
      0
      2
      0
      0
      0
      True
      True
      12
      0
      14

      300186488
      17
      2014
      0
      0
      1
      0
      0
      0
      0
      0
      0
      0
      True
      True
      12
      0
      12

      300186489
      16
      2014
      1
      0
      0
      0
      0
      0
      4
      2
      1
      1
      False
      False
      6
      0
      3

      300186490
      17
      2014
      0
      0
      0
      1
      0
      0
      0
      0
      0
      0
      True
      True
      10
      2
      6

      300186491
      17
      2014
      0
      1
      0
      0
      0
      0
      2
      2
      1
      1
      False
      False
      12
      0
      6

      300186492
      13
      2014
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      4
      1
      2

      300186493
      16
      2014
      1
      0
      0
      0
      0
      0
      4
      0
      0
      0
      False
      False
      5
      1
      1

      300186494
      13
      2014
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      3
      1
      2

      300186495
      16
      2014
      1
      0
      0
      0
      0
      0
      6
      2
      3
      -1
      False
      False
      6
      0
      3

      300186496
      16
      2014
      1
      0
      0
      0
      0
      0
      3
      3
      1
      2
      False
      False
      4
      2
      7

      300186497
      17
      2014
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1
      True
      False
      12
      0
      4

      300186498
      16
      2014
      1
      0
      0
      0
      0
      0
      2
      2
      2
      0
      False
      False
      5
      1
      3

      300186499
      18
      2014
      1
      0
      0
      0
      0
      0
      2
      0
      0
      0
      False
      False
      6
      0
      4

      300186500
      13
      2014
      1
      0
      0
      0
      0
      0
      1
      1
      1
      0
      False
      False
      5
      1
      2

      300186501
      16
      2014
      0
      0
      0
      0
      0
      1
      1
      1
      0
      1
      True
      False
      10
      2
      8

      300186502
      17
      2014
      0
      0
      0
      1
      0
      0
      3
      3
      2
      1
      False
      False
      10
      2
      10

      300186503
      13
      2014
      0
      1
      0
      0
      0
      0
      1
      1
      0
      1
      True
      False
      12
      0
      10

      300186504
      13
      2014
      0
      0
      1
      0
      0
      0
      1
      1
      1
      0
      False
      False
      12
      0
      6

      300186505
      16
      2014
      1
      0
      0
      0
      0
      0
      0
      0
      0
      0
      False
      False
      5
      0
      1

      300186506
      17
      2014
      1
      0
      0
      0
      0
      0
      2
      0
      1
      -1
      False
      False
      5
      1
      5

      300186507
      22
      2014
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      6
      0
      4

      300186508
      13
      2014
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1
      False
      False
      10
      2
      6

      300186509
      16
      2014
      1
      0
      0
      0
      0
      0
      0
      0
      0
      0
      False
      False
      5
      1
      4

      300186510
      16
      2014
      1
      0
      0
      0
      0
      0
      6
      4
      0
      4
      False
      False
      6
      0
      4

      300186511
      18
      2014
      1
      0
      0
      0
      0
      0
      1
      1
      1
      0
      False
      False
      5
      0
      2

      300186512
      19
      2014
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      5
      1
      2

      300186513
      18
      2014
      1
      0
      0
      0
      0
      0
      3
      1
      0
      1
      False
      False
      6
      0
      1

      300186514
      16
      2014
      1
      0
      0
      0
      0
      0
      7
      3
      3
      0
      False
      False
      5
      1
      1

      300186515
      17
      2014
      1
      0
      0
      0
      0
      0
      0
      0
      0
      0
      False
      False
      6
      0
      1

836 rows × 17 columns

```python
df_events_grp.columns
```

    Index(['HourGameStart', 'Year', 'Stage_1', 'Stage_2', 'Stage_3', 'Stage_4',
           'Stage_5', 'Stage_6', 'GoalsTotal', 'GoalDifference',
           'GoalDifferenceHalfTime', 'DeltaGoals', 'ExtraTime', 'Penalty', 'I',
           'IH', 'Y'],
          dtype='object')

We simply use MinMaxScaler as preprocessing

```python
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
df_events_grp[['HourGameStart','Year','GoalsTotal','GoalDifference','GoalDifferenceHalfTime','DeltaGoals']] = scaler.fit_transform(df_events_grp[['HourGameStart','Year','GoalsTotal','GoalDifference','GoalDifferenceHalfTime','DeltaGoals']])
```

Linear regression is used to predict the amount of yellow cards for the test data. The MSE is fairly lower than the one of the baseline model. The coefficient of determination (R squared, amount of explained variance) is 0.5 indicating a moderate model performance. Keep in mind that R squared values depend on the amount of features used (strictly increasing on number of features).

From Wiki (R squared is the amount of variance explained by the model)
\begin{align}
\mathit{R}^2 = \frac{\text{ESS}}{\text{TSS}}=
\frac{\displaystyle\sum\nolimits \left(\hat{y}_i- \overline{y}\right)^2}{\displaystyle\sum\nolimits \left(y_i - \overline{y}\right)^2}
\end{align}

```python
from sklearn.model_selection import train_test_split
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score

# train test split
# linear regression to predict y
X = df_events_grp.loc[:, 'HourGameStart':'IH']
#X = pd.concat([df_fouls.loc[:, 'StageRank':'IH'], df_fouls.loc[:, 'ALG':'ZAI']], axis=1)
Y = df_events_grp.loc[:, 'Y']

# transform to numpy array
X = X.as_matrix().astype(np.float)
Y = Y.as_matrix().astype(np.float)

# train/ test split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=40)

# fit regression model
regr = linear_model.LinearRegression()
regr.fit(X_train, y_train)

# compare RMSE and R value with base model
y_pred = regr.predict(X_test)

# base model would be the average y value
# needed for comparison
y_base_pred = np.zeros((len(y_pred)))
y_base_pred[:,] = y_train.mean()

print("LINEAR REGRESSION: Mean squared error: %.2f"
      % mean_squared_error(y_test, y_pred))
print('LINEAR REGRESSION: Variance score: %.2f' % r2_score(y_test, y_pred))

print("BASE: Mean squared error: %.2f"
      % mean_squared_error(y_test, y_base_pred))
# zero for sure, just added for completeness
print('BASE: Variance score: %.2f' % r2_score(y_test, y_base_pred))
```

    LINEAR REGRESSION: Mean squared error: 3.06
    LINEAR REGRESSION: Variance score: 0.50
    BASE: Mean squared error: 6.11
    BASE: Variance score: -0.00

To statistically analyze the model we also compute several statistics. The most important ones are

- T values: statistic for t test checking whether coefficient is zero
- p values: probability that coefficient is zero
- F statistic: is the group of features significant?

We can derive that only x2, x15, x16 are significant, i.e. Year, I (=Amount of substitutions) and IH (=Amount of half time substitutions) by observing the p values. Alternatively, this can be concluded by interpreting the confidence intervals spanning over zero for all other variables.

All together F-statistic prob is low enough. Hence, all variables together can be considered significant.

```python
import statsmodels.api as sm
from scipy import stats

X2 = sm.add_constant(X_train)
est = sm.OLS(y_train, X2)
est2 = est.fit()
print(est2.summary())
```

    C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\compat\pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.
      from pandas.core import datetools

                                OLS Regression Results
    ==============================================================================
    Dep. Variable:                      y   R-squared:                       0.461
    Model:                            OLS   Adj. R-squared:                  0.447
    Method:                 Least Squares   F-statistic:                     31.07
    Date:                Wed, 01 Aug 2018   Prob (F-statistic):           2.13e-63
    Time:                        09:15:23   Log-Likelihood:                -1162.0
    No. Observations:                 560   AIC:                             2356.
    Df Residuals:                     544   BIC:                             2425.
    Df Model:                          15
    Covariance Type:            nonrobust
    ==============================================================================
                     coef    std err          t      P>|t|      [0.025      0.975]
    ------------------------------------------------------------------------------
    const          0.0826      0.886      0.093      0.926      -1.658       1.824
    x1             0.2728      0.349      0.781      0.435      -0.413       0.959
    x2             3.5290      0.704      5.015      0.000       2.147       4.911
    x3            -0.7543      0.915     -0.824      0.410      -2.553       1.044
    x4             0.2847      0.948      0.300      0.764      -1.577       2.146
    x5            -0.3711      0.950     -0.390      0.696      -2.238       1.496
    x6            -0.8014      0.963     -0.833      0.405      -2.692       1.090
    x7            -0.8052      1.665     -0.484      0.629      -4.076       2.466
    x8             0.5436      1.061      0.512      0.609      -1.541       2.628
    x9            -0.7947      0.701     -1.133      0.258      -2.172       0.583
    x10           -0.3125      0.428     -0.730      0.465      -1.153       0.528
    x11           -0.3916      0.571     -0.685      0.493      -1.514       0.731
    x12           -0.0240      0.438     -0.055      0.956      -0.884       0.836
    x13            0.3682      0.501      0.734      0.463      -0.617       1.353
    x14            0.8808      0.683      1.289      0.198      -0.462       2.223
    x15            0.2900      0.077      3.768      0.000       0.139       0.441
    x16            0.4159      0.151      2.756      0.006       0.119       0.712
    ==============================================================================
    Omnibus:                      154.397   Durbin-Watson:                   1.967
    Prob(Omnibus):                  0.000   Jarque-Bera (JB):              660.338
    Skew:                           1.181   Prob(JB):                    4.07e-144
    Kurtosis:                       7.766   Cond. No.                     1.35e+17
    ==============================================================================

    Warnings:
    [1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
    [2] The smallest eigenvalue is 5.21e-31. This might indicate that there are
    strong multicollinearity problems or that the design matrix is singular.

To confirm a correlation between yellow cards and year, penalty, substitutions and half time substitutions we compute the Pearson correlation coefficient. The p-value corresponds to the probability to observe the left correlation coefficient randomly. All correlations are significant.

The positive correlation with year suggests that nowadays more yellow cards are given. This might be due to stricter rules or less fair play.

The correlation with substitutions is not obvious.

```python
from scipy.stats.stats import pearsonr

print(pearsonr(df_events_grp['Year'],Y)) # equivalent to: print(pearsonr(X[:,1],Y)) because not sensitive to scaling
print(pearsonr(df_events_grp['I'],Y)) # substitutions
print(pearsonr(df_events_grp['IH'],Y)) # half time substitutions
```

    (0.6280545772750047, 6.283851010000187e-93)
    (0.6277592949664524, 8.115135889320015e-93)
    (0.21070534342553665, 7.634463181895365e-10)

The correlation with year and substitutions can also be observed in a scatter plot. For penalty this does plot does not make sense as it is a binary decision.

```python
import numpy as np
import matplotlib.pyplot as plt

plt.figure()
plt.plot(df_events_grp['Year'],Y, "o")
plt.xlabel("Year (scaled)")
plt.ylabel("Yellow Cards")
plt.figure()
plt.plot(df_events_grp['I'],Y, "o")
plt.xlabel("Substitutions (scaled)")
plt.ylabel("Yellow Cards")
```

We try several ways to increase the performance

1. introduce regularization to tune our linear model (usually we need cross validation to tune the introduced hyperparameter values. However as we could not improve model performance we did not perform that step)
2. try a different ML model to increase accuracy
3. introduce team as one hot encoding feature to increase performance

But first, why not remove the unnecessary features and make the model more robust?

```python
X2 = df_events_grp.loc[:, ['Year', 'Penalty', 'IH', 'I']]
Y2 = df_events_grp.loc[:, 'Y']

# transform to numpy array
X2 = X2.as_matrix().astype(np.float)
Y2 = Y2.as_matrix().astype(np.float)

# train/ test split
X_train2, X_test2, y_train2, y_test2 = train_test_split(X2, Y2, test_size=0.33, random_state=40)

# fit regression model
regr = linear_model.LinearRegression()
regr.fit(X_train2, y_train2)

# compare RMSE and R value with base model
y_pred2 = regr.predict(X_test2)

# base model would be the average y value
# needed for comparison
y_base_pred = np.zeros((len(y_pred)))
y_base_pred[:,] = y_train.mean()

print("LINEAR REGRESSION: Mean squared error: %.2f"
      % mean_squared_error(y_test2, y_pred2))
print('LINEAR REGRESSION: Variance score: %.2f' % r2_score(y_test2, y_pred2))

print("BASE: Mean squared error: %.2f"
      % mean_squared_error(y_test2, y_base_pred))
# zero for sure, just added for completeness
print('BASE: Variance score: %.2f' % r2_score(y_test2, y_base_pred))
```

    LINEAR REGRESSION: Mean squared error: 3.19
    LINEAR REGRESSION: Variance score: 0.48
    BASE: Mean squared error: 6.11
    BASE: Variance score: -0.00

## Attempt 1: Regularization
As mentioned above, regularization fails to improve the linear model. In this case we only tried ridge regression. One could also employ lasso regression etc. to regularize which have the effect of feature selection or lower variable scale respectively.

```python
from sklearn.linear_model import Ridge

clf = Ridge(alpha=2)
clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)

print("REGULARIZATION: Mean squared error: %.2f"
      % mean_squared_error(y_test, y_pred))
print('REGULARIZATION: Variance score: %.2f' % r2_score(y_test, y_pred))
```

    REGULARIZATION: Mean squared error: 3.01
    REGULARIZATION: Variance score: 0.51

## Attempt 2a: Try a regression tree
Just another regression model. Leafes of the tree contain values for the specific subspace.

```python
from sklearn.tree import DecisionTreeRegressor

# fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=4)
regr_1.fit(X_train, y_train)
regr_2.fit(X_train, y_train)

# compare RMSE and R value with base model
y_pred = regr_1.predict(X_test)
y_pred_2 = regr_2.predict(X_test)

# base model would be the average y value
# needed for comparison
y_base_pred = np.zeros((len(y_pred)))
y_base_pred[:,] = y_train.mean()

print("DECISION TREE 1: Mean squared error: %.2f"
      % mean_squared_error(y_test, y_pred))
print('DECISION TREE 1: Variance score: %.2f' % r2_score(y_test, y_pred))

print("DECISION TREE 2: Mean squared error: %.2f"
      % mean_squared_error(y_test, y_pred_2))
print('DECISION TREE 2: Variance score: %.2f' % r2_score(y_test, y_pred_2))
```

    DECISION TREE 1: Mean squared error: 3.63
    DECISION TREE 1: Variance score: 0.41
    DECISION TREE 2: Mean squared error: 3.30
    DECISION TREE 2: Variance score: 0.46

## Attempt 2b: Neural Network

Sounds fancy but actually just a vanilla multilayer perceptron with only two layers.

```python
from sklearn.neural_network import MLPRegressor

nn = MLPRegressor(
    hidden_layer_sizes=(1,),  activation='relu', solver='adam', alpha=0.001, batch_size='auto',
    learning_rate='constant', learning_rate_init=0.01, power_t=0.5, max_iter=1000, shuffle=True,
    random_state=9, tol=0.0001, verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True,
    early_stopping=False, validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-08)

nn.fit(X_train, y_train)

y_pred = nn.predict(X_test)

print("NEURAL NETWORK: Mean squared error: %.2f"
      % mean_squared_error(y_test, y_pred))
print('NEURAL NETWORK: Variance score: %.2f' % r2_score(y_test, y_pred))
```

    NEURAL NETWORK: Mean squared error: 3.71
    NEURAL NETWORK: Variance score: 0.39

## Attempt 3: Add team as one hot encoding
We hot encode the teams of the specific match and hope to increase the accuracy

```python
df_teams_ohe = (pd.get_dummies(df_events['Home Team Initials'])+pd.get_dummies(df_events['Away Team Initials'])).fillna(value=0)
df_teams_ohe = pd.concat([df_teams_ohe,df_events['MatchID']],axis=1).groupby('MatchID').mean()
df_fouls = df_events_grp.join(df_teams_ohe)
df_fouls = df_fouls.reset_index()
df_fouls.drop(['MatchID'], 1,inplace=True)
df_fouls
```

      HourGameStart
      Year
      Stage_1
      Stage_2
      Stage_3
      Stage_4
      Stage_5
      Stage_6
      GoalsTotal
      GoalDifference
      ...
      TUN
      TUR
      UAE
      UKR
      URS
      URU
      USA
      WAL
      YUG
      ZAI

      0
      0.545455
      0.714286
      0
      1
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      1
      0.636364
      0.714286
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      2
      0.818182
      0.714286
      0
      0
      0
      0
      0
      1
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      3
      0.818182
      0.714286
      0
      0
      0
      1
      0
      0
      0.166667
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      4
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.166667
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      5
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.166667
      0.222222
      ...
      0
      0
      0
      0
      1
      0
      0
      0
      0
      0

      6
      0.545455
      0.714286
      0
      0
      1
      0
      0
      0
      0.000000
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      1
      0

      7
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      8
      0.545455
      0.714286
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      9
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      1
      0
      0
      0

      10
      0.909091
      0.714286
      0
      1
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      11
      0.545455
      0.714286
      1
      0
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      12
      0.545455
      0.714286
      1
      0
      0
      0
      0
      0
      0.166667
      0.222222
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      13
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.333333
      0.222222
      ...
      0
      0
      0
      0
      0
      1
      0
      0
      0
      0

      14
      0.545455
      0.714286
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      15
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      16
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      17
      0.545455
      0.714286
      0
      1
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      18
      0.909091
      0.714286
      0
      0
      1
      0
      0
      0
      0.416667
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      19
      0.545455
      0.714286
      1
      0
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      20
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.333333
      0.444444
      ...
      0
      0
      0
      0
      1
      0
      0
      0
      0
      0

      21
      0.545455
      0.714286
      1
      0
      0
      0
      0
      0
      0.166667
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      22
      0.545455
      0.714286
      1
      0
      0
      0
      0
      0
      0.166667
      0.222222
      ...
      0
      0
      1
      0
      0
      0
      0
      0
      0
      0

      23
      0.545455
      0.714286
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      1
      0

      24
      0.545455
      0.714286
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      25
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      26
      0.909091
      0.714286
      0
      1
      0
      0
      0
      0
      0.416667
      0.333333
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      27
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      28
      0.909091
      0.714286
      1
      0
      0
      0
      0
      0
      0.166667
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      29
      0.545455
      0.714286
      1
      0
      0
      0
      0
      0
      0.000000
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      806
      0.454545
      1.000000
      1
      0
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      1
      0
      0
      0
      0

      807
      0.181818
      1.000000
      0
      1
      0
      0
      0
      0
      0.166667
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      808
      0.545455
      1.000000
      0
      0
      1
      0
      0
      0
      0.000000
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      809
      0.454545
      1.000000
      1
      0
      0
      0
      0
      0
      0.333333
      0.222222
      ...
      0
      0
      0
      0
      0
      1
      0
      0
      0
      0

      810
      0.545455
      1.000000
      0
      0
      0
      1
      0
      0
      0.000000
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      811
      0.545455
      1.000000
      0
      1
      0
      0
      0
      0
      0.166667
      0.222222
      ...
      0
      0
      0
      0
      0
      1
      0
      0
      0
      0

      812
      0.181818
      1.000000
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      813
      0.454545
      1.000000
      1
      0
      0
      0
      0
      0
      0.333333
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      814
      0.181818
      1.000000
      1
      0
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      815
      0.454545
      1.000000
      1
      0
      0
      0
      0
      0
      0.500000
      0.222222
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      816
      0.454545
      1.000000
      1
      0
      0
      0
      0
      0
      0.250000
      0.333333
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      817
      0.545455
      1.000000
      0
      1
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      1
      0
      0
      0

      818
      0.454545
      1.000000
      1
      0
      0
      0
      0
      0
      0.166667
      0.222222
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      819
      0.636364
      1.000000
      1
      0
      0
      0
      0
      0
      0.166667
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      820
      0.181818
      1.000000
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      821
      0.454545
      1.000000
      0
      0
      0
      0
      0
      1
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      822
      0.545455
      1.000000
      0
      0
      0
      1
      0
      0
      0.250000
      0.333333
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      823
      0.181818
      1.000000
      0
      1
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      824
      0.181818
      1.000000
      0
      0
      1
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      825
      0.454545
      1.000000
      1
      0
      0
      0
      0
      0
      0.000000
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      826
      0.545455
      1.000000
      1
      0
      0
      0
      0
      0
      0.166667
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      827
      1.000000
      1.000000
      1
      0
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      828
      0.181818
      1.000000
      0
      1
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      829
      0.454545
      1.000000
      1
      0
      0
      0
      0
      0
      0.000000
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      830
      0.454545
      1.000000
      1
      0
      0
      0
      0
      0
      0.500000
      0.444444
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      831
      0.636364
      1.000000
      1
      0
      0
      0
      0
      0
      0.083333
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      832
      0.727273
      1.000000
      1
      0
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      1
      0
      0
      0

      833
      0.636364
      1.000000
      1
      0
      0
      0
      0
      0
      0.250000
      0.111111
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      834
      0.454545
      1.000000
      1
      0
      0
      0
      0
      0
      0.583333
      0.333333
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

      835
      0.545455
      1.000000
      1
      0
      0
      0
      0
      0
      0.000000
      0.000000
      ...
      0
      0
      0
      0
      0
      0
      0
      0
      0
      0

836 rows × 99 columns

```python
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
df_fouls[['HourGameStart','Year','GoalsTotal','GoalDifference','GoalDifferenceHalfTime','DeltaGoals']] = scaler.fit_transform(df_fouls[['HourGameStart','Year','GoalsTotal','GoalDifference','GoalDifferenceHalfTime','DeltaGoals']])
```

Unfortunately, adding the team did not improve the model performance. Probably due to shortage of data.

```python
from sklearn.model_selection import train_test_split
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score

# train test split
# linear regression to predict y
X = pd.concat([df_fouls.loc[:, 'HourGameStart':'IH'], df_fouls.loc[:, 'ALG':'ZAI']], axis=1)
Y = df_events_grp.loc[:, 'Y']

# transform to numpy array
X = X.as_matrix().astype(np.float)
Y = Y.as_matrix().astype(np.float)

# train/ test split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=40)

# base model would be the average y value
# needed for comparison
y_base_pred = np.zeros((len(y_pred)))
y_base_pred[:,] = y_train.mean()

# fit regression model
regr = linear_model.LinearRegression()
regr.fit(X_train, y_train)

# compare RMSE and R value with base model
y_pred = regr.predict(X_test)
print("LINEAR REGRESSION: Mean squared error: %.2f"
      % mean_squared_error(y_test, y_pred))
print('LINEAR REGRESSION: Variance score: %.2f' % r2_score(y_test, y_pred))

print("BASE: Mean squared error: %.2f"
      % mean_squared_error(y_test, y_base_pred))
# zero for sure, just added for completeness
print('BASE: Variance score: %.2f' % r2_score(y_test, y_base_pred))
```

    LINEAR REGRESSION: Mean squared error: 4.07
    LINEAR REGRESSION: Variance score: 0.33
    BASE: Mean squared error: 6.11
    BASE: Variance score: -0.00

# Predict Red Cards
Can we also predict whether a red card was given? This is a classification task.

```python
f = {'HourGameStart':['mean'],
     #'Home Team Goals':['mean'], # not symmetric -> throw out
     #'Away Team Goals':['mean'],
     #'Half-time Home Goals':['mean'],
     #'Half-time Away Goals':['mean'],
     'Year':['mean'],
     'Stage_1':['mean'],
     'Stage_2':['mean'],
     'Stage_3':['mean'],
     'Stage_4':['mean'],
     'Stage_5':['mean'],
     'Stage_6':['mean'],
     'GoalsTotal':['mean'],
     'GoalDifference':['mean'],
     'GoalDifferenceHalfTime':['mean'],
     'DeltaGoals':['mean'],
     'ExtraTime':['mean'],
     'Penalty':['mean'],
     'I':['sum'], #substitutions
     'IH':['sum'], #substitutions half time
     'Y':['sum'],
     'R':['sum'],
     'RSY':['sum']
    }

df_events_grp = df_events_ohe.groupby(['MatchID']).agg(f)
df_events_grp.columns = df_events_grp.columns.get_level_values(0)

# create column indicating whether red cards were given
df_events_grp['R_total'] = df_events_grp.R + df_events_grp.RSY
df_events_grp = df_events_grp.assign(R_flag = lambda x : x.R_total > 0)
df_events_grp = df_events_grp.drop(columns=['R', 'RSY','R_total'])
df_events_grp
```

      HourGameStart
      Year
      Stage_1
      Stage_2
      Stage_3
      Stage_4
      Stage_5
      Stage_6
      GoalsTotal
      GoalDifference
      GoalDifferenceHalfTime
      DeltaGoals
      ExtraTime
      Penalty
      I
      IH
      Y
      R_flag

      MatchID

      25
      17
      1990
      0
      1
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      3
      0
      5
      True

      26
      18
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      3
      1
      4
      True

      27
      20
      1990
      0
      0
      0
      0
      0
      1
      1
      1
      0
      1
      False
      False
      2
      1
      4
      True

      28
      20
      1990
      0
      0
      0
      1
      0
      0
      2
      0
      0
      0
      True
      True
      3
      1
      7
      True

      29
      21
      1990
      1
      0
      0
      0
      0
      0
      2
      0
      0
      0
      False
      False
      4
      0
      5
      False

      30
      21
      1990
      1
      0
      0
      0
      0
      0
      2
      2
      1
      1
      False
      False
      4
      0
      6
      True

      31
      17
      1990
      0
      0
      1
      0
      0
      0
      0
      0
      0
      0
      True
      True
      3
      0
      5
      True

      42
      21
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      3
      1
      1
      False

      43
      17
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      1
      0
      False
      False
      2
      2
      5
      False

      48
      21
      1990
      1
      0
      0
      0
      0
      0
      3
      1
      0
      1
      False
      False
      3
      1
      9
      True

      55
      21
      1990
      0
      1
      0
      0
      0
      0
      1
      1
      0
      1
      True
      False
      4
      0
      1
      False

      56
      17
      1990
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      4
      0
      0
      False

      57
      17
      1990
      1
      0
      0
      0
      0
      0
      2
      2
      0
      2
      False
      False
      1
      2
      1
      False

      66
      21
      1990
      1
      0
      0
      0
      0
      0
      4
      2
      2
      0
      False
      False
      2
      2
      3
      True

      73
      17
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      1
      0
      False
      False
      4
      0
      4
      False

      74
      21
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      3
      0
      2
      False

      75
      21
      1990
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      2
      1
      4
      False

      102
      17
      1990
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1
      True
      False
      4
      0
      6
      False

      103
      21
      1990
      0
      0
      1
      0
      0
      0
      5
      1
      0
      1
      True
      False
      3
      1
      4
      False

      108
      17
      1990
      1
      0
      0
      0
      0
      0
      3
      1
      0
      1
      False
      False
      4
      0
      3
      False

      111
      21
      1990
      1
      0
      0
      0
      0
      0
      4
      4
      2
      2
      False
      False
      3
      1
      3
      False

      114
      17
      1990
      1
      0
      0
      0
      0
      0
      2
      0
      0
      0
      False
      False
      2
      0
      4
      False

      119
      17
      1990
      1
      0
      0
      0
      0
      0
      2
      2
      0
      2
      False
      False
      3
      0
      3
      False

      120
      17
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      3
      1
      1
      False

      127
      17
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      2
      1
      0
      False

      128
      21
      1990
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      4
      0
      4
      False

      129
      21
      1990
      0
      1
      0
      0
      0
      0
      5
      3
      1
      2
      False
      False
      1
      1
      5
      False

      150
      21
      1990
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      4
      0
      3
      False

      151
      21
      1990
      1
      0
      0
      0
      0
      0
      2
      0
      0
      0
      False
      False
      3
      1
      2
      False

      152
      17
      1990
      1
      0
      0
      0
      0
      0
      0
      0
      0
      0
      False
      False
      4
      0
      2
      False

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      300186486
      16
      2014
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      6
      0
      2
      False

      300186487
      13
      2014
      0
      1
      0
      0
      0
      0
      2
      0
      0
      0
      True
      True
      12
      0
      14
      False

      300186488
      17
      2014
      0
      0
      1
      0
      0
      0
      0
      0
      0
      0
      True
      True
      12
      0
      12
      False

      300186489
      16
      2014
      1
      0
      0
      0
      0
      0
      4
      2
      1
      1
      False
      False
      6
      0
      3
      True

      300186490
      17
      2014
      0
      0
      0
      1
      0
      0
      0
      0
      0
      0
      True
      True
      10
      2
      6
      False

      300186491
      17
      2014
      0
      1
      0
      0
      0
      0
      2
      2
      1
      1
      False
      False
      12
      0
      6
      False

      300186492
      13
      2014
      1
      0
      0
      0
      0
      0
      1
      1
      0
      1
      False
      False
      4
      1
      2
      False

      300186493
      16
      2014
      1
      0
      0
      0
      0
      0
      4
      0
      0
      0
      False
      False
      5
      1
      1
      False

      300186494
      13
      2014
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      3
      1
      2
      False

      300186495
      16
      2014
      1
      0
      0
      0
      0
      0
      6
      2
      3
      -1
      False
      False
      6
      0
      3
      False

      300186496
      16
      2014
      1
      0
      0
      0
      0
      0
      3
      3
      1
      2
      False
      False
      4
      2
      7
      True

      300186497
      17
      2014
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1
      True
      False
      12
      0
      4
      False

      300186498
      16
      2014
      1
      0
      0
      0
      0
      0
      2
      2
      2
      0
      False
      False
      5
      1
      3
      False

      300186499
      18
      2014
      1
      0
      0
      0
      0
      0
      2
      0
      0
      0
      False
      False
      6
      0
      4
      False

      300186500
      13
      2014
      1
      0
      0
      0
      0
      0
      1
      1
      1
      0
      False
      False
      5
      1
      2
      False

      300186501
      16
      2014
      0
      0
      0
      0
      0
      1
      1
      1
      0
      1
      True
      False
      10
      2
      8
      False

      300186502
      17
      2014
      0
      0
      0
      1
      0
      0
      3
      3
      2
      1
      False
      False
      10
      2
      10
      False

      300186503
      13
      2014
      0
      1
      0
      0
      0
      0
      1
      1
      0
      1
      True
      False
      12
      0
      10
      False

      300186504
      13
      2014
      0
      0
      1
      0
      0
      0
      1
      1
      1
      0
      False
      False
      12
      0
      6
      False

      300186505
      16
      2014
      1
      0
      0
      0
      0
      0
      0
      0
      0
      0
      False
      False
      5
      0
      1
      False

      300186506
      17
      2014
      1
      0
      0
      0
      0
      0
      2
      0
      1
      -1
      False
      False
      5
      1
      5
      False

      300186507
      22
      2014
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      6
      0
      4
      False

      300186508
      13
      2014
      0
      1
      0
      0
      0
      0
      3
      1
      0
      1
      False
      False
      10
      2
      6
      False

      300186509
      16
      2014
      1
      0
      0
      0
      0
      0
      0
      0
      0
      0
      False
      False
      5
      1
      4
      False

      300186510
      16
      2014
      1
      0
      0
      0
      0
      0
      6
      4
      0
      4
      False
      False
      6
      0
      4
      False

      300186511
      18
      2014
      1
      0
      0
      0
      0
      0
      1
      1
      1
      0
      False
      False
      5
      0
      2
      False

      300186512
      19
      2014
      1
      0
      0
      0
      0
      0
      3
      1
      1
      0
      False
      False
      5
      1
      2
      False

      300186513
      18
      2014
      1
      0
      0
      0
      0
      0
      3
      1
      0
      1
      False
      False
      6
      0
      1
      False

      300186514
      16
      2014
      1
      0
      0
      0
      0
      0
      7
      3
      3
      0
      False
      False
      5
      1
      1
      False

      300186515
      17
      2014
      1
      0
      0
      0
      0
      0
      0
      0
      0
      0
      False
      False
      6
      0
      1
      True

836 rows × 18 columns

The decision tree is unable to outperform the base model. With higher tree depths the train test gap increases. The trees heavily overfit.

```python
from sklearn import tree
from sklearn.metrics import accuracy_score

# train test split
# linear regression to predict y
X = df_events_grp.loc[:, 'HourGameStart':'Y']
Y = df_events_grp.loc[:, 'R_flag']

# transform to numpy array
X = X.as_matrix().astype(np.float)
Y = Y.as_matrix().astype(np.float)

# train/ test split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)

def decision_tree_accuracy(depth):
    # fit decision tree
    clf = tree.DecisionTreeClassifier(max_depth=depth)
    clf = clf.fit(X_train, y_train)

    # compute accuracy
    y_pred = clf.predict(X_test)
    score = clf.score(X_test, y_test)

    train_score = accuracy_score(clf.predict(X_train), y_train)

    return score, train_score

# for comparison also compute accuracy for base model (always output zero)
y_base_pred = np.zeros((len(y_test))) # zeros as most matches are without red cards
base_score = accuracy_score(y_base_pred, y_test)

dec_tree_accuracy = np.array([(i, decision_tree_accuracy(i)[0], decision_tree_accuracy(i)[1]) for i in range(1,15)])
base_accuracy = np.array([base_score for i in range(1,15)])

plt.figure(figsize=(12,6))

plt.plot(dec_tree_accuracy[:,0], dec_tree_accuracy[:,1]*100, label="Decision Tree Test")
plt.plot(dec_tree_accuracy[:,0], dec_tree_accuracy[:,2]*100, label="Decision Tree Train")
plt.plot(dec_tree_accuracy[:,0], base_accuracy*100, label="Base")

plt.legend()
plt.title("Accuracies depending on tree depth")
plt.xlabel("Decision tree depth")
plt.ylabel("Accuracy (%)")

plt.show()
```

A little less overfitting, but still unable to achieve a significantly higher accuracy: random forests.

```python
from sklearn.ensemble import RandomForestClassifier

def random_forest_accuracy(depth):
    # fit decision tree
    clf = RandomForestClassifier(max_depth=depth, n_estimators=10)
    clf = clf.fit(X_train, y_train)

    # compute accuracy
    y_pred = clf.predict(X_test)
    score = clf.score(X_test, y_test)

    train_score = accuracy_score(clf.predict(X_train), y_train)

    return score, train_score

rf_tree_accuracy = np.array([(i, random_forest_accuracy(i)[0], random_forest_accuracy(i)[1]) for i in range(1,15)])

plt.figure(figsize=(12,6))

plt.plot(rf_tree_accuracy[:,0], rf_tree_accuracy[:,1]*100, label="Random Forest Test")
plt.plot(rf_tree_accuracy[:,0], rf_tree_accuracy[:,2]*100, label="Random Forest Train")
plt.plot(rf_tree_accuracy[:,0], base_accuracy*100, label="Base")

plt.legend()
plt.title("Accuracies depending on tree depth")
plt.xlabel("Decision tree depth")
plt.ylabel("Accuracy (%)")

plt.show()
```

k-NN does not perform significantly better either.

```python
from sklearn.neighbors import KNeighborsClassifier

def knn_accuracy(depth):
    # fit decision tree
    clf = KNeighborsClassifier(depth)
    clf = clf.fit(X_train, y_train)

    # compute accuracy
    y_pred = clf.predict(X_test)
    score = clf.score(X_test, y_test)

    train_score = accuracy_score(clf.predict(X_train), y_train)

    return score, train_score

knn_accuracy = np.array([(i, knn_accuracy(i)[0], knn_accuracy(i)[1]) for i in range(1,15)])

plt.figure(figsize=(12,6))

plt.plot(knn_accuracy[:,0], knn_accuracy[:,1]*100, label="k-NN Test")
plt.plot(knn_accuracy[:,0], knn_accuracy[:,2]*100, label="k-NN Train")
plt.plot(knn_accuracy[:,0], base_accuracy*100, label="Base")

plt.legend()
plt.title("Accuracies depending on k")
plt.xlabel("k")
plt.ylabel("Accuracy (%)")

plt.show()
```

```python

```