<jupyter_start><jupyter_text>PGA Driving Distance Over the Years> A look at how PGA driving distance has changed over the Years- toc: false - badges: true- comments: true- categories: [jupyter, pandas, pga]- image: images/pga-driving-distance.png<jupyter_code>#collapse-hide

import pandas as pd
import altair as alt
# Analyzing 20 Driving Distances

tables = pd.read_html('https://www.pgatour.com/stats/stat.101.2020.html')
drives = tables[1]
drives.head(10)
drives.describe()['AVG.']<jupyter_output><empty_output><jupyter_text>Average Driving Distance Since 1980<jupyter_code>#collapse-hide

# Create a List of Years
years = pd.date_range(start='1/1/1980', end=pd.to_datetime('today') +
                      pd.offsets.DateOffset(month=12, day=31), freq='Y').year

# Loop Through URL's and create a Dataframe

d = []

for year in years:
    tables = pd.read_html(f'https://www.pgatour.com/stats/stat.101.y{year}.html')
    
    try:
        drives = tables[1].loc[:100]
        
        d.append({'Year': year, 'Average Drive': drives['AVG.'].mean()})
    except:
        pass

df = pd.DataFrame(d)
df['Average Drive'] = df['Average Drive'].astype(int)
#collapse-hide

# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
                        fields=['Year'], empty='none')

# The basic line
line = alt.Chart(df).mark_line(interpolate='basis').encode(
    alt.X('Year:N'),
    alt.Y('Average Drive:Q', scale=alt.Scale(zero=False)),
)

# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(df).mark_point().encode(
    x='Year:N',
    opacity=alt.value(0),
).add_selection(
    nearest
)

# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
    opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)

# Draw text labels near the points, and highlight based on selection
text = line.mark_text(align='left', dx=5, dy=-5).encode(
    text=alt.condition(nearest, 'Average Drive:Q', alt.value(' '))
)

# Draw a rule at the location of the selection
rules = alt.Chart(df).mark_rule(color='gray').encode(
    x='Year:N',
).transform_filter(
    nearest
)

# Put the five layers into a chart and bind the data
alt.layer(
    line, selectors, points, rules, text
).properties(
    width=850, height=500
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.142974527526705, 'fraction_numerical': 0.018077239112571898, 'mean_word_length': 4.083507306889353, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '412248', 'license': 'Apache-2.0', 'n_tokens_mistral': 859, 'n_tokens_neox': 809, 'n_words': 211, 'path': '_notebooks/2020-10-18-analyzing-pga-driving-distance-with-pandas.ipynb', 'repo_name': 'gardnmi/fastai-blog', 'size': '2434'}