0

I'm new to Python and I'm trying to do some calcualtions on footbal scores Data frame. Is there a way to convert football scores (eg.3-2, 2-0..) from object dtype to a number, so I can perform some numerical operations on them?

Thank you in advance.

Bojan Pavlović
  • 103
  • 4
  • 15
  • 2
    can you provide sample code, expected input and output? – Amit Nanaware Apr 22 '21 at 10:39
  • Are they stored in string form, i.e. `"3-2"` ? – Sid Apr 22 '21 at 10:44
  • Does this help? https://stackoverflow.com/questions/39173813/pandas-convert-dtype-object-to-int#39216001 – Sid Apr 22 '21 at 10:47
  • Thanks for the answers. I didn't explain well enough. So I have a column with results stored in string form (i.e. "2-4", "1-1"..) from all the matches in the season and now I would like to sum all the goals in the season. – Bojan Pavlović Apr 22 '21 at 21:50

1 Answers1

0

In the future, provide a sample dataset with expected input and output.

You can split the column on the '-' charachter. Convert the columns to int and do a groupby to sum the point totals.

Given:

print(df)
  Team Result Score
0    A      W   3-0
1    A      W   3-2
2    A      L   2-4
3    A      L   1-2
4    B      W   1-0
5    B      L   0-1
6    B      T   1-1
7    B      L   2-3

Code:

import pandas as pd


df = pd.DataFrame([['A','W','3-0'],
                   ['A','W','3-2'],
                   ['A','L','2-4'],
                   ['A','L','1-2'],
                   ['B','W','1-0'],
                   ['B','L','0-1'],
                   ['B','T','1-1'],
                   ['B','L','2-3']], columns=['Team','Result','Score'])

df[['Team Points','Opp Points']] = df['Score'].str.split('-',expand=True)
df[['Team Points','Opp Points']] = df[['Team Points','Opp Points']].astype(int)

df_sum = df.groupby(['Team'])['Team Points'].sum()

Output:

print(df)
  Team Result Score  Team Points  Opp Points
0    A      W   3-0            3           0
1    A      W   3-2            3           2
2    A      L   2-4            2           4
3    A      L   1-2            1           2
4    B      W   1-0            1           0
5    B      L   0-1            0           1
6    B      T   1-1            1           1
7    B      L   2-3            2           3

Sum:

print(df_sum)
Team
A    9
B    4
Name: Team Points, dtype: int32
chitown88
  • 27,527
  • 4
  • 30
  • 59