In [66]:
# nbi:hide_in

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
%matplotlib inline
import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed, interact_manual
import nbinteract as nbi

sns.set()
sns.set_context('talk')
np.set_printoptions(threshold=20, precision=2, suppress=True)
pd.options.display.max_rows = 7
pd.options.display.max_columns = 8
pd.set_option('precision', 2)
# This option stops scientific notation for pandas
# pd.set_option('display.float_format', '{:.2f}'.format)

In [79]:
# nbi:hide_in
def normal(mean, sd):
    '''Returns 1000 points drawn at random fron N(mean, sd)'''
    return np.random.normal(mean, sd, 1000)


def df_interact(df, nrows=7, ncols=7):
    '''
    Outputs sliders that show rows and columns of df
    '''
    def peek(row=0, col=0):
        return df.iloc[row:row + nrows, col:col + ncols]
    if len(df.columns) <= ncols:
        interact(peek, row=(0, len(df) - nrows, nrows), col=fixed(0))
    else:
        interact(peek,
                 row=(0, len(df) - nrows, nrows),
                 col=(0, len(df.columns) - ncols))
    print('({} rows, {} columns) total'.format(df.shape[0], df.shape[1]))

In [104]:
# nbi:hide_in
videos = pd.read_csv('https://github.com/SamLau95/nbinteract/raw/master/notebooks/youtube_trending.csv',
                     parse_dates=['publish_time'],
                     index_col='publish_time')

YouTube Trending Video Statistics


In [100]:
df_interact(videos)


(2435 rows, 8 columns) total

In [1]:
# nbi:left
# nbi:hide_in
options = {
    'title': 'Views for Trending Videos',
    'xlabel': 'Date Trending',
    'ylabel': 'Views',
    'animation_duration': 500,
}

def xs(channel):
    return videos.loc[videos['channel_title'] == channel].index

def ys(xs):
    return videos.loc[xs, 'views']

nbi.scatter(xs, ys,
            channel=videos['channel_title'].unique()[9:15],
            options=options)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-703bb16c0da2> in <module>()
     14     return videos.loc[xs, 'views']
     15 
---> 16 nbi.scatter(xs, ys,
     17             channel=videos['channel_title'].unique()[9:15],
     18             options=options)

NameError: name 'nbi' is not defined

In [98]:
# nbi:right
# nbi:hide_in
options={
    'ylabel': 'Proportion per Unit',
    'bins': 100,
}


def values(col):
    vals = videos[col]
    return vals[vals < vals.quantile(0.8)]

nbi.hist(values, col=widgets.ToggleButtons(options=['views', 'likes', 'dislikes', 'comment_count']), options=options)



In [ ]: