In [8]:
import pandas as pd
import pymysql

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='BaseballAnalytics')
df = pd.read_sql(sql = 'SELECT * FROM TeamSchedules', con = conn, index_col = 'index')
conn.close()
df


Out[8]:
DN GB Gm Inn Loss Opp R RA Rank Rk ... Tm WL Win StreakNum CurrentStreak isWin isHome isDay DateNum DayOfWeek
index
0 D 1.0 1 9.00 Tanaka TOR 1 6 4 1 ... NYY L Hutchison -1 NaN 0 1 1 2015-04-06 Monday
1 N 0.5 2 8.50 Loup TOR 4 3 2 2 ... NYY W Betances 1 -1 1 1 0 2015-04-08 Wednesday
2 N 1.0 3 9.00 Sabathia TOR 3 6 4 3 ... NYY L Norris -1 1 0 1 0 2015-04-09 Thursday
3 N 2.0 4 19 Rogers BOS 5 6 4 4 ... NYY L Wright -2 -1 0 1 0 2015-04-10 Friday
4 D 3.0 5 9.00 Warren BOS 4 8 5 5 ... NYY L Kelly -3 -2 0 1 1 2015-04-11 Saturday
5 N 2.0 6 8.50 Buchholz BOS 14 4 5 6 ... NYY W Tanaka 1 -3 1 1 0 2015-04-12 Sunday
6 N 2.0 7 9.00 Hunter BAL 6 5 4 7 ... NYY W Pineda 2 1 1 0 0 2015-04-13 Monday
7 N 3.0 8 8.50 Sabathia BAL 3 4 5 8 ... NYY L Gonzalez -1 2 0 0 0 2015-04-14 Tuesday
8 N 3.0 9 8.50 Carpenter BAL 5 7 5 9 ... NYY L Brach -2 -1 0 0 0 2015-04-15 Wednesday
9 N 3.0 10 9.00 Jepsen TBR 5 4 5 10 ... NYY W Betances 1 -2 1 0 0 2015-04-17 Friday
10 N 2.0 11 9.00 Odorizzi TBR 9 0 5 11 ... NYY W Tanaka 2 1 1 0 0 2015-04-18 Saturday
11 D 1.0 12 9.00 Andriese TBR 5 3 3 12 ... NYY W Pineda 3 2 1 0 1 2015-04-19 Sunday
12 N 2.0 13 8.50 Sabathia DET 1 2 3 13 ... NYY L Simon -1 3 0 0 0 2015-04-20 Monday
13 N 2.0 14 9.00 Lobstein DET 5 2 2 14 ... NYY W Eovaldi 1 -1 1 0 0 2015-04-21 Tuesday
14 N 1.0 15 9.00 Price DET 13 4 2 15 ... NYY W Warren 2 1 1 0 0 2015-04-22 Wednesday
15 D 0 16 9.00 Gorzelanny DET 2 1 1 16 ... NYY W Betances 3 2 1 0 1 2015-04-23 Thursday
16 N 0 17 8.50 deGrom NYM 6 1 1 17 ... NYY W Pineda 4 3 1 1 0 2015-04-24 Friday
17 D 0 18 9.00 Sabathia NYM 2 8 1 18 ... NYY L Harvey -1 4 0 1 1 2015-04-25 Saturday
18 N 0 19 8.50 Niese NYM 6 4 1 19 ... NYY W Shreve 1 -1 1 1 0 2015-04-26 Sunday
19 N up 1.0 20 8.50 Gomes TBR 4 1 1 20 ... NYY W Wilson 2 1 1 1 0 2015-04-27 Monday
20 N up 2.0 21 8.50 Odorizzi TBR 4 2 1 21 ... NYY W Whitley 3 2 1 1 0 2015-04-28 Tuesday
21 D up 1.0 22 13 Shreve TBR 2 3 1 22 ... NYY L Gomes -1 3 0 1 1 2015-04-29 Wednesday
22 N up 1.0 23 9.00 Tazawa BOS 3 2 1 23 ... NYY W Rogers 1 -1 1 0 0 2015-05-01 Friday
23 D up 2.0 24 9.00 Miley BOS 4 2 1 24 ... NYY W Eovaldi 2 1 1 0 1 2015-05-02 Saturday
24 N up 3.0 25 9.00 Kelly BOS 8 5 1 25 ... NYY W Warren 3 2 1 0 0 2015-05-03 Sunday
25 N up 2.0 26 8.50 Martin TOR 1 3 1 26 ... NYY L Dickey -1 3 0 0 0 2015-05-04 Monday
26 N up 3.0 27 9.00 Estrada TOR 6 3 1 27 ... NYY W Pineda 1 -1 1 0 0 2015-05-05 Tuesday
27 N up 2.0 28 8.50 Sabathia TOR 1 5 1 28 ... NYY L Buehrle -1 1 0 0 0 2015-05-06 Wednesday
28 N up 3.0 29 8.50 Tillman BAL 4 3 1 29 ... NYY W Eovaldi 1 -1 1 1 0 2015-05-07 Thursday
29 N up 3.0 30 8.50 Gonzalez BAL 5 4 1 30 ... NYY W Betances 2 1 1 1 0 2015-05-08 Friday
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
132 None None 133 None None ARI Game Preview, Matchups, and Tickets None 133 ... COL 8:40 pm None 0 0 0 1 0 2015-09-02 Wednesday
133 None None 134 None None SFG Game Preview, Matchups, and Tickets None 134 ... COL 8:40 pm None 0 0 0 1 0 2015-09-03 Thursday
134 None None 135 None None SFG Game Preview, Matchups, and Tickets None 135 ... COL 8:40 pm None 0 0 0 1 0 2015-09-04 Friday
135 None None 136 None None SFG Game Preview, Matchups, and Tickets None 136 ... COL 8:10 pm None 0 0 0 1 0 2015-09-05 Saturday
136 None None 137 None None SFG Game Preview, Matchups, and Tickets None 137 ... COL 4:10 pm None 0 0 0 1 0 2015-09-06 Sunday
137 None None 138 None None SDP Game Preview, Matchups, and Tickets None 138 ... COL 4:10 pm None 0 0 0 0 0 2015-09-07 Monday
138 None None 139 None None SDP Game Preview, Matchups, and Tickets None 139 ... COL 10:10 pm None 0 0 0 0 0 2015-09-08 Tuesday
139 None None 140 None None SDP Game Preview, Matchups, and Tickets None 140 ... COL 9:10 pm None 0 0 0 0 0 2015-09-09 Wednesday
140 None None 141 None None SEA Game Preview, Matchups, and Tickets None 141 ... COL 10:10 pm None 0 0 0 0 0 2015-09-11 Friday
141 None None 142 None None SEA Game Preview, Matchups, and Tickets None 142 ... COL 9:10 pm None 0 0 0 0 0 2015-09-12 Saturday
142 None None 143 None None SEA Game Preview, Matchups, and Tickets None 143 ... COL 4:10 pm None 0 0 0 0 0 2015-09-13 Sunday
143 None None 144 None None LAD Game Preview, Matchups, and Tickets None 144 ... COL 10:10 pm None 0 0 0 0 0 2015-09-14 Monday
144 None None 145 None None LAD Game Preview, Matchups, and Tickets None 145 ... COL 10:10 pm None 0 0 0 0 0 2015-09-15 Tuesday
145 None None 146 None None LAD Game Preview, Matchups, and Tickets None 146 ... COL 10:10 pm None 0 0 0 0 0 2015-09-16 Wednesday
146 None None 147 None None SDP Game Preview, Matchups, and Tickets None 147 ... COL 8:40 pm None 0 0 0 1 0 2015-09-18 Friday
147 None None 148 None None SDP Game Preview, Matchups, and Tickets None 148 ... COL 8:10 pm None 0 0 0 1 0 2015-09-19 Saturday
148 None None 149 None None SDP Game Preview, Matchups, and Tickets None 149 ... COL 4:10 pm None 0 0 0 1 0 2015-09-20 Sunday
149 None None 150 None None PIT Game Preview, Matchups, and Tickets None 150 ... COL 8:40 pm None 0 0 0 1 0 2015-09-21 Monday
150 None None 151 None None PIT Game Preview, Matchups, and Tickets None 151 ... COL 8:40 pm None 0 0 0 1 0 2015-09-22 Tuesday
151 None None 152 None None PIT Game Preview, Matchups, and Tickets None 152 ... COL 8:40 pm None 0 0 0 1 0 2015-09-23 Wednesday
152 None None 153 None None PIT Game Preview, Matchups, and Tickets None 153 ... COL 3:10 pm None 0 0 0 1 0 2015-09-24 Thursday
153 None None 154 None None LAD Game Preview, Matchups, and Tickets None 154 ... COL 8:10 pm None 0 0 0 1 0 2015-09-25 Friday
154 None None 155 None None LAD Game Preview, Matchups, and Tickets None 155 ... COL 8:10 pm None 0 0 0 1 0 2015-09-26 Saturday
155 None None 156 None None LAD Game Preview, Matchups, and Tickets None 156 ... COL 4:10 pm None 0 0 0 1 0 2015-09-27 Sunday
156 None None 157 None None ARI Game Preview, Matchups, and Tickets None 157 ... COL 9:40 pm None 0 0 0 0 0 2015-09-29 Tuesday
157 None None 158 None None ARI Game Preview, Matchups, and Tickets None 158 ... COL 9:40 pm None 0 0 0 0 0 2015-09-30 Wednesday
158 None None 159 None None ARI Game Preview, Matchups, and Tickets None 159 ... COL 9:40 pm None 0 0 0 0 0 2015-10-01 Thursday
159 None None 160 None None SFG Game Preview, Matchups, and Tickets None 160 ... COL 10:15 pm None 0 0 0 0 0 2015-10-02 Friday
160 None None 161 None None SFG Game Preview, Matchups, and Tickets None 161 ... COL 4:05 pm None 0 0 0 0 0 2015-10-03 Saturday
161 None None 162 None None SFG Game Preview, Matchups, and Tickets None 162 ... COL 3:05 pm None 0 0 0 0 0 2015-10-04 Sunday

4858 rows × 22 columns


In [9]:
df.columns


Out[9]:
Index(['DN', 'GB', 'Gm', 'Inn', 'Loss', 'Opp', 'R', 'RA', 'Rank', 'Rk', 'Save', 'Time', 'Tm', 'WL', 'Win', 'StreakNum', 'CurrentStreak', 'isWin', 'isHome', 'isDay', 'DateNum', 'DayOfWeek'], dtype='object')

In [17]:
from datetime import date

today = date.today()

dfData = df[df['DateNum'] < today]

In [26]:
dfTeam = dfData.groupby('Tm')

In [30]:
import numpy as np
dfTeam.apply(lambda x: sum(x['R']))


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-30-b92428dabcda> in <module>()
      1 import numpy as np
----> 2 dfTeam.apply(lambda x: sum(x['R']))

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/groupby.py in apply(self, func, *args, **kwargs)
    660         # ignore SettingWithCopy here in case the user mutates
    661         with option_context('mode.chained_assignment',None):
--> 662             return self._python_apply_general(f)
    663 
    664     def _python_apply_general(self, f):

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/groupby.py in _python_apply_general(self, f)
    664     def _python_apply_general(self, f):
    665         keys, values, mutated = self.grouper.apply(f, self._selected_obj,
--> 666                                                    self.axis)
    667 
    668         return self._wrap_applied_output(keys, values,

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/groupby.py in apply(self, f, data, axis)
   1288             # group might be modified
   1289             group_axes = _get_axes(group)
-> 1290             res = f(group)
   1291             if not _is_indexed_like(res, group_axes):
   1292                 mutated = True

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/groupby.py in f(g)
    656         @wraps(func)
    657         def f(g):
--> 658             return func(g, *args, **kwargs)
    659 
    660         # ignore SettingWithCopy here in case the user mutates

<ipython-input-30-b92428dabcda> in <lambda>(x)
      1 import numpy as np
----> 2 dfTeam.apply(lambda x: sum(x['R']))

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [ ]: