Home > front end >  DataFrame values, if statement
DataFrame values, if statement

Time:09-17

I'm trying to create a shot map by scraping data from a website to find football data. I want the hometeam's shots to be on one side of the pitch, and the awayteam's shots to be on the other side. Any help? I'm trying to solve the problem by using an if-statement with if h/a = h.... and else....

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from mplsoccer.pitch import Pitch
from pandas.core.indexes.base import Index
        
text_color = 'w'
data = pd.read_csv('shotmaps.csv')        
        
fig, ax = plt.subplots(figsize=(13,8.5))
fig.set_facecolor('#22312b')        
ax.patch.set_facecolor('#22312b')
        
pitch = Pitch(half=False, positional=True, shade_middle=True, positional_color='black', line_color='black', axis=True, label=True, tick=True)
pitch.draw(ax=ax)

plt.title('Manchester United vs Newcastle', fontsize=24, c=text_color)

if data['H/A'].eq('h').any():
   plt.scatter(data['x-coordinate']*120,data['y-coordinate']*80, c = '#ea6969', s=70)

if data['H/A'].eq('a').any():
   plt.gca().invert_xaxis()
   plt.scatter(data['x-coordinate']*120,data['y-coordinate']*80, c = 'blue', s=70)

This is the csv I'm getting my DataFrame from:

    Unnamed: 0  Minute               Player   Situation               Team  \
0            0       8    Cristiano Ronaldo    OpenPlay  Manchester United   
1            1      10    Cristiano Ronaldo    OpenPlay  Manchester United   
2            2      18       Raphael Varane  FromCorner  Manchester United   
3            3      22    Cristiano Ronaldo    OpenPlay  Manchester United   
4            4      25      Bruno Fernandes    OpenPlay  Manchester United   
5            5      28      Bruno Fernandes    OpenPlay  Manchester United   
6            6      29      Bruno Fernandes    OpenPlay  Manchester United   
7            7      29         Jadon Sancho    OpenPlay  Manchester United   
8            8      31         Jadon Sancho    OpenPlay  Manchester United   
9            9      31        Harry Maguire  FromCorner  Manchester United   
10          10      38    Cristiano Ronaldo    OpenPlay  Manchester United   
11          11      42        Nemanja Matic    OpenPlay  Manchester United   
12          12      46      Mason Greenwood    OpenPlay  Manchester United   
13          13      46    Cristiano Ronaldo    OpenPlay  Manchester United   
14          14      53        Nemanja Matic    OpenPlay  Manchester United   
15          15      58      Bruno Fernandes    OpenPlay  Manchester United   
16          16      61    Cristiano Ronaldo    OpenPlay  Manchester United   
17          17      65       Raphael Varane  FromCorner  Manchester United   
18          18      75      Mason Greenwood  FromCorner  Manchester United   
19          19      79      Bruno Fernandes    OpenPlay  Manchester United   
20          20      91        Jesse Lingard    OpenPlay  Manchester United   
21          21       6            Joelinton    OpenPlay   Newcastle United   
22          22      14         Matt Ritchie    OpenPlay   Newcastle United   
23          23      22       Miguel Almirón    OpenPlay   Newcastle United   
24          24      22         Matt Ritchie    OpenPlay   Newcastle United   
25          25      27          Joe Willock    OpenPlay   Newcastle United   
26          26      35  Allan Saint-Maximin    SetPiece   Newcastle United   
27          27      55     Javier Manquillo    OpenPlay   Newcastle United   
28          28      66            Joelinton    OpenPlay   Newcastle United   
29          29      68  Allan Saint-Maximin    OpenPlay   Newcastle United   
30          30      78  Allan Saint-Maximin    OpenPlay   Newcastle United   
31          31      79          Joe Willock  FromCorner   Newcastle United   
32          32      86            Joelinton    OpenPlay   Newcastle United   

          xG       Result  x-coordinate  y-coordinate H/A  
0   0.057108  MissedShots         0.978         0.338   h  
1   0.039677  MissedShots         0.972         0.720   h  
2   0.219654  MissedShots         0.930         0.616   h  
3   0.026905  BlockedShot         0.766         0.521   h  
4   0.006393  MissedShots         0.494         0.800   h  
5   0.009298  MissedShots         0.718         0.804   h  
6   0.021526  MissedShots         0.744         0.452   h  
7   0.369672  MissedShots         0.892         0.514   h  
8   0.128696  BlockedShot         0.851         0.527   h  
9   0.042829    SavedShot         0.905         0.424   h  
10  0.108275  BlockedShot         0.940         0.342   h  
11  0.014675  BlockedShot         0.732         0.382   h  
12  0.015693    SavedShot         0.802         0.176   h  
13  0.887859         Goal         0.976         0.516   h  
14  0.015525  MissedShots         0.697         0.488   h  
15  0.030891  BlockedShot         0.787         0.418   h  
16  0.301787         Goal         0.923         0.632   h  
17  0.070338  MissedShots         0.905         0.519   h  
18  0.027712  BlockedShot         0.862         0.285   h  
19  0.031612         Goal         0.756         0.499   h  
20  0.369022         Goal         0.865         0.522   h  
21  0.070895  MissedShots         0.860         0.675   a  
22  0.016308  BlockedShot         0.767         0.720   a  
23  0.011347  BlockedShot         0.744         0.310   a  
24  0.014497  MissedShots         0.784         0.699   a  
25  0.068061  MissedShots         0.927         0.330   a  
26  0.015108  MissedShots         0.765         0.707   a  
27  0.264944         Goal         0.933         0.328   a  
28  0.060307    SavedShot         0.872         0.660   a  
29  0.006815  MissedShots         0.698         0.726   a  
30  0.020886  BlockedShot         0.739         0.479   a  
31  0.013166    SavedShot         0.706         0.464   a  
32  0.059875  MissedShots         0.912         0.593   a  

CodePudding user response:

First, split your dataframe into home and away teams:

home_team = df.loc[df["H/A"] == "h"]
away_team = df.loc[df["H/A"] == "a"]

Now, let's say the home team is attacking left-right. An x-coordinate of, say, 0.9 indicates that the home team shot from a location 90% up the pitch from their goal, which would be at an x-coordinate of 0.9 * pitch_length.

On the other hand, the away team shooting at 0.9 would indicate a location 90% up the pitch from their goal, or 10% up the pitch from the home team's goal. In other words, x = (1 - 0.9) * pitch_length

So do that.

plt.scatter(home_team['x-coordinate']*120, home_team['y-coordinate']*80, c = '#ea6969', s=70)
plt.scatter((1-away_team['x-coordinate'])*120, away_team['y-coordinate']*80, c = 'blue', s=70)

enter image description here

  • Related