%pylab inline
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
import pandas as pd
pd.version.version
'0.11.0'
np.version.version
'1.7.1'
pd.set_option("max_rows", 100)
pd.set_option("max_columns", 25)
np.set_printoptions(linewidth=200)
figsize(12,9)
from IPython.display import HTML
HTML('<iframe src="http://botliga.de/datenquellen" width=1200 height=400></iframe>')
#!curl -O http://dl.dropbox.com/u/90393/botliga/ergebnisse-1963-2010.zip
#!unzip -qq ergebnisse-*.zip
#cd tippkick/
#!psql -q < tippkick.sql
import psycopg2
conn = psycopg2.connect("dbname=tippkick")
import pandas.io.sql as psql
vereine = psql.frame_query('select * from vereine', con=conn)
vereine.head()
id | name | |
---|---|---|
0 | 1 | Werder Bremen |
1 | 2 | Borussia Dortmund |
2 | 3 | 1. FC Saarbrücken |
3 | 4 | 1. FC Köln |
4 | 5 | TSV 1860 München |
spiele = psql.frame_query('select * from spiele', con=conn)
spiele.head()
heim | gast | saison | tore_heim | tore_gast | spieltag | zeit | |
---|---|---|---|---|---|---|---|
0 | 1 | 2 | 1963 | 3 | 2 | 1 | 1963-08-24 17:00:00 |
1 | 3 | 4 | 1963 | 0 | 2 | 1 | 1963-08-24 17:00:00 |
2 | 5 | 6 | 1963 | 1 | 1 | 1 | 1963-08-24 17:00:00 |
3 | 7 | 8 | 1963 | 1 | 1 | 1 | 1963-08-24 17:00:00 |
4 | 9 | 10 | 1963 | 2 | 0 | 1 | 1963-08-24 17:00:00 |
temp = pd.merge(spiele, vereine, left_on="heim", right_on="id")
temp.head()
<class 'pandas.core.frame.DataFrame'> Int64Index: 5 entries, 0 to 4 Data columns (total 9 columns): heim 5 non-null values gast 5 non-null values saison 5 non-null values tore_heim 5 non-null values tore_gast 5 non-null values spieltag 5 non-null values zeit 5 non-null values id 5 non-null values name 5 non-null values dtypes: datetime64[ns](1), int64(7), object(1)
temp2 = pd.merge(temp, vereine, left_on="gast", right_on="id")
temp2.head()
<class 'pandas.core.frame.DataFrame'> Int64Index: 5 entries, 0 to 4 Data columns (total 11 columns): heim 5 non-null values gast 5 non-null values saison 5 non-null values tore_heim 5 non-null values tore_gast 5 non-null values spieltag 5 non-null values zeit 5 non-null values id_x 5 non-null values name_x 5 non-null values id_y 5 non-null values name_y 5 non-null values dtypes: datetime64[ns](1), int64(8), object(2)
df = temp2["name_y name_x saison tore_heim tore_gast spieltag zeit".split()]
df.columns = "heim gast saison tore_heim tore_gast spieltag zeit".split()
df
<class 'pandas.core.frame.DataFrame'> Int64Index: 14630 entries, 0 to 14629 Data columns (total 7 columns): heim 14630 non-null values gast 14630 non-null values saison 14630 non-null values tore_heim 14630 non-null values tore_gast 14630 non-null values spieltag 14630 non-null values zeit 14630 non-null values dtypes: datetime64[ns](1), int64(4), object(2)
df.set_index("zeit")
<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 14630 entries, 1963-08-24 17:00:00 to 2010-08-21 15:30:00 Data columns (total 6 columns): heim 14630 non-null values gast 14630 non-null values saison 14630 non-null values tore_heim 14630 non-null values tore_gast 14630 non-null values spieltag 14630 non-null values dtypes: int64(4), object(2)
df["tore"] = df["tore_heim"] + df["tore_gast"]
saison_spieltag = df.set_index(["saison", "spieltag"])
print saison_spieltag.head().to_string()
heim gast tore_heim tore_gast zeit tore saison spieltag 1963 1 Borussia Dortmund Werder Bremen 3 2 1963-08-24 17:00:00 5 1964 29 Borussia Dortmund Werder Bremen 3 0 1965-05-08 16:00:00 3 1965 32 Borussia Dortmund Werder Bremen 1 0 1966-05-14 16:00:00 1 1966 24 Borussia Dortmund Werder Bremen 2 1 1967-03-04 16:00:00 3 1967 32 Borussia Dortmund Werder Bremen 2 1 1968-05-11 15:30:00 3
grouped = saison_spieltag["tore"].groupby(level=0)
grouped.sum().plot(kind="bar")
<matplotlib.axes.AxesSubplot at 0x4b7e390>
df[df["tore"] > 10].sort("tore", ascending=False)
<class 'pandas.core.frame.DataFrame'> Int64Index: 15 entries, 11542 to 2052 Data columns (total 8 columns): heim 15 non-null values gast 15 non-null values saison 15 non-null values tore_heim 15 non-null values tore_gast 15 non-null values spieltag 15 non-null values zeit 15 non-null values tore 15 non-null values dtypes: datetime64[ns](1), int64(5), object(2)
from IPython.display import YouTubeVideo
YouTubeVideo('VfvWX4bHSb0')
df.ix[2558]
heim Hamburger SV gast Bayern München saison 1989 tore_heim 4 tore_gast 0 spieltag 7 zeit 1989-08-31 19:30:00 tore 4 Name: 2558, dtype: object
df.ix[0]["zeit"].hour
17
df["uhrzeit"]=df["zeit"].apply(lambda x: str(x).split()[1])
startzeiten = df["uhrzeit"].value_counts()
startzeiten
15:30:00 9670 20:00:00 2349 16:00:00 650 17:30:00 487 17:00:00 248 15:00:00 231 19:30:00 198 18:00:00 177 20:30:00 148 14:30:00 104 20:15:00 86 18:30:00 76 16:30:00 49 14:15:00 35 19:00:00 25 15:15:00 24 14:00:00 18 18:15:00 12 18:45:00 7 17:45:00 6 06:00:00 4 19:45:00 4 15:45:00 4 20:45:00 2 16:45:00 2 09:30:00 2 17:25:00 2 06:30:00 2 21:00:00 2 11:30:00 1 19:15:00 1 16:15:00 1 22:30:00 1 17:40:00 1 19:55:00 1 dtype: int64
startzeiten.plot(kind="barh")
<matplotlib.axes.AxesSubplot at 0x4b48c50>
df[df["uhrzeit"] == "19:30:00"].sort("saison", ascending=False).head(10)
<class 'pandas.core.frame.DataFrame'> Int64Index: 10 entries, 2634 to 2404 Data columns (total 9 columns): heim 10 non-null values gast 10 non-null values saison 10 non-null values tore_heim 10 non-null values tore_gast 10 non-null values spieltag 10 non-null values zeit 10 non-null values tore 10 non-null values uhrzeit 10 non-null values dtypes: datetime64[ns](1), int64(5), object(3)