{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "mdf = pd.read_csv('matches.csv')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('deliveries.csv')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
match_idinningbatting_teambowling_teamoverballbatsmannon_strikerbowleris_super_over...bye_runslegbye_runsnoball_runspenalty_runsbatsman_runsextra_runstotal_runsplayer_dismisseddismissal_kindfielder
011Sunrisers HyderabadRoyal Challengers Bangalore11DA WarnerS DhawanTS Mills0...0000000NaNNaNNaN
\n", "

1 rows × 21 columns

\n", "
" ], "text/plain": [ " match_id inning batting_team bowling_team over \\\n", "0 1 1 Sunrisers Hyderabad Royal Challengers Bangalore 1 \n", "\n", " ball batsman non_striker bowler is_super_over ... bye_runs \\\n", "0 1 DA Warner S Dhawan TS Mills 0 ... 0 \n", "\n", " legbye_runs noball_runs penalty_runs batsman_runs extra_runs \\\n", "0 0 0 0 0 0 \n", "\n", " total_runs player_dismissed dismissal_kind fielder \n", "0 0 NaN NaN NaN \n", "\n", "[1 rows x 21 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(1)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "matchID = 1" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "df = df[(df.match_id == 1) & (df.inning == 2)]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([nan, 'Mandeep Singh', 'CH Gayle', 'KM Jadhav', 'TM Head',\n", " 'Sachin Baby', 'STR Binny', 'SR Watson', 'S Aravind', 'TS Mills',\n", " 'YS Chahal'], dtype=object)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.player_dismissed.unique()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "players = list(df.batsman.unique())" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['CH Gayle',\n", " 'Mandeep Singh',\n", " 'TM Head',\n", " 'KM Jadhav',\n", " 'SR Watson',\n", " 'Sachin Baby',\n", " 'STR Binny',\n", " 'S Aravind',\n", " 'YS Chahal',\n", " 'TS Mills',\n", " 'A Choudhary']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "players" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "runs_ls = []\n", "balls_ls = []\n", "\n", "for player in players:\n", " temp_df = df[df.batsman == player]\n", " \n", " if len(temp_df) > 0: \n", " runs_ls.append(sum(temp_df.batsman_runs))\n", " balls_ls.append(len(temp_df))\n", " else:\n", " runs_ls.append(0)\n", " balls_ls.append(0)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "players_dismissed = list(df.player_dismissed.unique())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[nan,\n", " 'Mandeep Singh',\n", " 'CH Gayle',\n", " 'KM Jadhav',\n", " 'TM Head',\n", " 'Sachin Baby',\n", " 'STR Binny',\n", " 'SR Watson',\n", " 'S Aravind',\n", " 'TS Mills',\n", " 'YS Chahal']" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "players_dismissed" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['CH Gayle',\n", " 'Mandeep Singh',\n", " 'TM Head',\n", " 'KM Jadhav',\n", " 'SR Watson',\n", " 'Sachin Baby',\n", " 'STR Binny',\n", " 'S Aravind',\n", " 'YS Chahal',\n", " 'TS Mills',\n", " 'A Choudhary']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "players" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([nan, 'bowled', 'caught', 'run out'], dtype=object)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.dismissal_kind.unique()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "out_no_ls = []\n", "fielder_ls = []\n", "bowler_ls = []\n", "\n", "for player in players:\n", " if player in players_dismissed:\n", " out_no_ls.append('out')\n", " temp_df = df[df.player_dismissed == player]\n", " out_type = temp_df['dismissal_kind'].item()\n", " \n", " if out_type == 'caught':\n", " fielder_ls.append(temp_df['fielder'].item())\n", " bowler_ls.append(temp_df['bowler'].item())\n", " elif out_type == 'bowled':\n", " fielder_ls.append('')\n", " bowler_ls.append(temp_df['bowler'].item())\n", " else:\n", " fielder_ls.append( 'runout' + '(' + str(temp_df['fielder'].item()) + ')')\n", " bowler_ls.append('')\n", " \n", " else:\n", " out_no_ls.append('no')\n", " fielder_ls.append('')\n", " bowler_ls.append('')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(['out', 'out', 'out', 'out', 'out', 'out', 'out', 'out', 'out', 'out', 'no'],\n", " ['DA Warner',\n", " '',\n", " 'Yuvraj Singh',\n", " 'runout(BCJ Cutting)',\n", " 'MC Henriques',\n", " 'MC Henriques',\n", " 'Yuvraj Singh',\n", " '',\n", " 'runout(CJ Jordan (sub))',\n", " 'DA Warner',\n", " ''],\n", " ['DJ Hooda',\n", " 'Rashid Khan',\n", " 'Rashid Khan',\n", " '',\n", " 'A Nehra',\n", " 'Bipul Sharma',\n", " 'B Kumar',\n", " 'A Nehra',\n", " '',\n", " 'B Kumar',\n", " ''])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out_no_ls, fielder_ls, bowler_ls" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "runs_balls_list = []\n", "\n", "for i in range(len(players)):\n", " if out_no_ls[i] == 'no':\n", " runs_balls_list.append( str(runs_ls[i]) + '*' + ' (' + str(balls_ls[i]) + ' )' )\n", " else:\n", " runs_balls_list.append( str(runs_ls[i]) + ' (' + str(balls_ls[i]) + ' )' ) " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CH Gayle DA Warner DJ Hooda 32 (23 )\n", "Mandeep Singh Rashid Khan 24 (16 )\n", "TM Head Yuvraj Singh Rashid Khan 30 (22 )\n", "KM Jadhav runout(BCJ Cutting) 31 (18 )\n", "SR Watson MC Henriques A Nehra 22 (17 )\n", "Sachin Baby MC Henriques Bipul Sharma 1 (3 )\n", "STR Binny Yuvraj Singh B Kumar 11 (10 )\n", "S Aravind A Nehra 0 (2 )\n", "YS Chahal runout(CJ Jordan (sub)) 3 (7 )\n", "TS Mills DA Warner B Kumar 6 (3 )\n", "A Choudhary 6* (2 )\n" ] } ], "source": [ "for i in range(len(players)):\n", " print(players[i], ' ', fielder_ls[i], bowler_ls[i], runs_balls_list[i])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "scorecard_dict = {'player':players, 'fielder':fielder_ls, 'bowler':bowler_ls, 'runs':runs_balls_list}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
playerfielderbowlerruns
0CH GayleDA WarnerDJ Hooda32 (23 )
1Mandeep SinghRashid Khan24 (16 )
2TM HeadYuvraj SinghRashid Khan30 (22 )
3KM Jadhavrunout(BCJ Cutting)31 (18 )
4SR WatsonMC HenriquesA Nehra22 (17 )
5Sachin BabyMC HenriquesBipul Sharma1 (3 )
6STR BinnyYuvraj SinghB Kumar11 (10 )
7S AravindA Nehra0 (2 )
8YS Chahalrunout(CJ Jordan (sub))3 (7 )
9TS MillsDA WarnerB Kumar6 (3 )
10A Choudhary6* (2 )
\n", "
" ], "text/plain": [ " player fielder bowler runs\n", "0 CH Gayle DA Warner DJ Hooda 32 (23 )\n", "1 Mandeep Singh Rashid Khan 24 (16 )\n", "2 TM Head Yuvraj Singh Rashid Khan 30 (22 )\n", "3 KM Jadhav runout(BCJ Cutting) 31 (18 )\n", "4 SR Watson MC Henriques A Nehra 22 (17 )\n", "5 Sachin Baby MC Henriques Bipul Sharma 1 (3 )\n", "6 STR Binny Yuvraj Singh B Kumar 11 (10 )\n", "7 S Aravind A Nehra 0 (2 )\n", "8 YS Chahal runout(CJ Jordan (sub)) 3 (7 )\n", "9 TS Mills DA Warner B Kumar 6 (3 )\n", "10 A Choudhary 6* (2 )" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame(scorecard_dict)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "scorecard_df = pd.DataFrame(scorecard_dict)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "# scorecard_df.to_csv('scorecard.csv')" ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }