Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created July 18, 2016 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/2da73e126d40deac4588dfaa37531d09 to your computer and use it in GitHub Desktop.
Save kurozumi/2da73e126d40deac4588dfaa37531d09 to your computer and use it in GitHub Desktop.
【Python】Pandasを使ってJリーグチームの平均入場者数を計算する方法
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#pip install pandas lxml html5lib BeautifulSoup4"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"year = 2016\n",
"url = \"http://data.jleague.jp/SFMS01/search?competition_years={year}&team_ids=42&home_away_select=0&section_months=1&section_months=2&section_months=3&section_months=4&section_months=5&section_months=6&section_months=7&section_months=8&section_months=9&section_months=10&section_months=11&section_months=12\".format(year=year)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"databases = pd.io.html.read_html(url)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"database = databases[0]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# シーズン途中なのでNanの入場者数行を削除\n",
"database = database.dropna()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# ホームの入場者数だけ取得\n",
"home = database[database[\"ホーム\"]==\"岡山\"]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"8990.916666666666"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 平均入場者数\n",
"home[\"入場者数\"].mean()"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [py35]",
"language": "python",
"name": "Python [py35]"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment