<jupyter_start><jupyter_text>The Office - Character MentionsThe `schrutepy` package provides the script for all seasons of The Office. [The Office - example analysis](https://nbviewer.jupyter.org/github/zgana/data-science-one-offs/blob/master/NLP-Analysis-of-The-Office.ipynb?flush_cache=trueCharacter-character-mentions)<jupyter_code>import altair as alt
import pandas as pd

from schrutepy import schrutepy

alt.themes.enable("fivethirtyeight")
MAIN_CHARACTERS = [
    "Michael", "Jim", "Pam", "Dwight", 
    "Phyllis", "Stanley", "Meredith",
    "Oscar", "Angela", "Kevin",
    "Ryan", "Toby", "Kelly", 
]

def subset_data(full_dataset, MAIN_CHARACTERS):
    subset = full_dataset[(full_dataset.character.isin(MAIN_CHARACTERS)) & (full_dataset.text.notna())]
    
    subset = subset.assign(
        keep = subset.apply(lambda x: True if any(c for c in MAIN_CHARACTERS if c in 
                                                  x.text) else False, axis=1)
    )

    df = subset[subset.keep==1].reset_index(drop=True)

    for c in MAIN_CHARACTERS:
        df[c] = df.apply(lambda x: 1 if c in x.text else 0, axis=1)
        
    return df
full_dataset = schrutepy.load_schrute()
df = subset_data(full_dataset, MAIN_CHARACTERS)
# This looks like our standard df
# This df is "wide"
# Each character-season takes up 1 row, and who/how many times that character talks to is stored in columns
mentions_by_season = df.pivot_table(
    index=["character", "season"], 
    values=MAIN_CHARACTERS, 
    aggfunc="sum").reset_index()

mentions_by_season.head()
# Compare how this df looks with the previous one
# This df is "long"
# Each character-season-talked is a row, and the number of times is stored in the column
mentions_by_season_long = mentions_by_season.melt(id_vars=["character", "season"], 
                                                  var_name="talks_to")
mentions_by_season_long.head()
# Maybe we want to know how many mentions there are across ALL seasons
mentions_long = (mentions_by_season_long.groupby(["character", "talks_to"])
                 .agg({"value": "sum"})
                 .reset_index()
                )

mentions_long.head()<jupyter_output><empty_output><jupyter_text>Visualize: Character Mentions<jupyter_code>def make_bar_chart_by_season(df, character="Michael"):
    chart = (alt.Chart(df[df.character==character])
             .mark_bar()
             .encode(
                 x=alt.X("talks_to:N", title="character",
                         sort=alt.EncodingSortField(field="talks_to", order="ascending"),
                        axis=alt.Axis(labelAngle=-90)),
                 y=alt.Y("sum(value):Q", title="# times"),
                 column="season:O",
                 color="talks_to:N",
             ).properties(title=f"Who does {character} talk to the most?")
             .configure(background="white")
            )
    
    display(chart)
make_bar_chart_by_season(mentions_by_season_long, character="Michael")
make_bar_chart_by_season(mentions_by_season_long[mentions_by_season_long.season==9], character="Dwight")
def make_bar_chart(df, character="Michael"):
    chart = (alt.Chart(df[df.character==character])
             .mark_bar()
             .encode(
                 x=alt.X("talks_to:N", title="character",
                         sort=alt.EncodingSortField(field="value", order="descending"),
                        axis=alt.Axis(labelAngle=-90)),
                 y=alt.Y("sum(value):Q", title="# times"),
                 color=alt.Color("talks_to:N", title="")
             ).properties(title=f"Who does {character} talk to the most?")
             .configure(background="white")
            )
    
    display(chart)
make_bar_chart(mentions_long, character="Jim")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12463460005314908, 'fraction_numerical': 0.0029231995748073346, 'mean_word_length': 2.9208333333333334, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '528493', 'license': 'Apache-2.0', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1159, 'n_words': 241, 'path': 'notebooks/the-office.ipynb', 'repo_name': 'tiffanychu90/visualize-spotify', 'size': '3763'}