Skip to content

Instantly share code, notes, and snippets.

@Shinichi-Nakagawa
Created December 31, 2017 13:12
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 Shinichi-Nakagawa/1378bc2cde8a08d10276d4478d91dd76 to your computer and use it in GitHub Desktop.
Save Shinichi-Nakagawa/1378bc2cde8a08d10276d4478d91dd76 to your computer and use it in GitHub Desktop.
pitchpxで入手したデータをデータ種別毎にまとめるサンプル
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# ファイルの元path\n",
"path_data = Path('./data/')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def concat_csv(prefix):\n",
" file_list = [x for x in path_data.glob('{}*.csv'.format(prefix))]\n",
" values = []\n",
" for _file in file_list:\n",
" with open(_file, 'r') as f:\n",
" reader = csv.reader(f)\n",
" try:\n",
" header = next(reader)\n",
" except StopIteration as e:\n",
" continue\n",
" for value in reader:\n",
" values.append(value)\n",
" _df = pd.DataFrame([dict(zip(header, v)) for v in values])\n",
" _df.to_csv('./data/{}.csv'.format(prefix), index=False)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"concat_csv('mlbam_atbat_2017')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"concat_csv('mlbam_game_2017')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"concat_csv('mlbam_boxscore_2017')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"concat_csv('mlbam_action_2017')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"concat_csv('mlbam_coach_2017')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"concat_csv('mlbam_umpire_2017')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"concat_csv('mlbam_player_2017')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"concat_csv('mlbam_pitch_2017')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment