Upload Copy_of_Dataset_Collection_&_Structure-3.ipynb

#4
Owner

1. New Functionalities: Aggregation & Analysis

Data cleaning to grouping and aggregating data.

A. Orange Cap Winners (Highest Runs per Season)

To find the highest run-scorer for each season, the logic involves grouping by season and batter, summing the runs, and then extracting the top scorer.

  • Logic Implemented:
  1. Group By: groupby(['Year', 'Batter']) to segregate data by season and player.
  2. Aggregate: .sum()['Batter Runs'] to calculate total runs for each player in each season.
  3. Sort & Extract: Sort values descending and pick the top one for each year.

Sample Code Pattern:

orange_cap = df.groupby(['Year', 'Batter'])['Batter Runs'].sum().reset_index()
orange_cap = orange_cap.sort_values(['Year', 'Batter Runs'], ascending=[True, False])
orange_cap_winners = orange_cap.drop_duplicates(subset=['Year'], keep='first')

B. Most Sixes per Season

This requires filtering the data first to find only sixes, then counting them.

  • Logic Implemented:
  1. Filter: df[df['is_six'] == 1] (using the feature you created).
  2. Group By: groupby(['Year', 'Batter']).
  3. Count: .count() or .size() to find the total number of sixes.

Sample Code Pattern:

sixes_data = df[df['is_six'] == 1]
most_sixes = sixes_data.groupby(['Year', 'Batter'])['is_six'].count().reset_index()
most_sixes = most_sixes.sort_values(['Year', 'is_six'], ascending=[True, False])
most_sixes_winners = most_sixes.drop_duplicates(subset=['Year'], keep='first')

2. Key Pandas Functions for "Showing Graphs Properly"

When using libraries like Matplotlib or Seaborn to visualize this data, the structure of your DataFrame is critical. These functions are the "bridge" between raw data and clean graphs:

1. groupby()

  • Description: This is the core of aggregation. It splits your data into buckets (e.g., "2008 data", "2009 data"). Without this, you cannot calculate "total runs" or "total sixes"—you would only have individual ball-by-ball data.

2. reset_index() (Crucial for Plotting)

  • Why it's important for Graphs: When you use groupby(), Pandas turns your grouping columns (like Year and Batter) into an Index. Most plotting libraries (Seaborn, Plotly) prefer these to be regular columns so you can pass them as x='Year' or hue='Batter'.
  • Usage: df.groupby(...).sum().reset_index() flattens the table back into a standard format.

3. sort_values()

  • Why it's important for Graphs: Graphs often look messy if bars are random. This function ensures your data is ordered (e.g., from highest runs to lowest runs) before you plot it, making the graph easy to read and interpret.

4. drop_duplicates()

  • Why it's important for Graphs: For your "Orange Cap" question, you only want one bar per year (the winner). After sorting your data, drop_duplicates(subset=['Year'], keep='first') removes all the other batsmen, leaving only the winner for each season to be plotted.

5. pivot() or unstack()

  • Description: If you want to create a heatmap or a multi-line chart (e.g., Year on X-axis, Teams on Y-axis), these functions reshape your data from a "long" format to a "wide" format, which is often required for these specific chart types.

image

v2005 changed pull request status to merged

Sign up or log in to comment