<jupyter_start><jupyter_text>_Lambda School Data Science_ Sequence your narrativeCreate a sequence of visualizations inspired by [<NAME>'s 200 Countries, 200 Years, 4 Minutes](https://www.youtube.com/watch?v=jbkSRLYSojo).Using this [data from Gapminder](https://github.com/open-numbers/ddf--gapminder--systema_globalis/):- https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--income_per_person_gdppercapita_ppp_inflation_adjusted--by--geo--time.csv- https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--life_expectancy_years--by--geo--time.csv- https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--entities--geo--country.csv- https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--concepts.csv Stretch goals- [ipywidgets](https://github.com/jupyter-widgets/ipywidgets)- [Matplotlib animation](https://matplotlib.org/examples/animation/index.html)- [Connected scatter plots](http://www.thefunctionalart.com/2012/09/in-praise-of-connected-scatter-plots.html)- [Idyll markup language](https://idyll-lang.org/) for "scrollytelling"<jupyter_code>!pip install --upgrade seaborn
%matplotlib inline
import seaborn as sns
assert sns.__version__ == '0.9.0'
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


income_url='https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--income_per_person_gdppercapita_ppp_inflation_adjusted--by--geo--time.csv'
lifespan_url='https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--life_expectancy_years--by--geo--time.csv'
population_url='https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--datapoints--population_total--by--geo--time.csv'
entities_url='https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--entities--geo--country.csv'
concepts_url='https://raw.githubusercontent.com/open-numbers/ddf--gapminder--systema_globalis/master/ddf--concepts.csv'

income = pd.read_csv(income_url)
lifespan = pd.read_csv(lifespan_url)
population = pd.read_csv(population_url)
entities = pd.read_csv(entities_url)
concepts = pd.read_csv(concepts_url)
pd.options.display.max_columns = None
pd.options.display.max_rows = 50<jupyter_output><empty_output><jupyter_text>Merge Data<jupyter_code>df = pd.merge(income, lifespan)
variables = ['country', 'name', 'world_6region']
df = pd.merge(df, population)
# entities[entities.country=='abw'].T

# Preview
pd.merge(df, entities[variables], 
         how='inner', left_on='geo', right_on='country').head()
df = pd.merge(df, entities[variables], 
              how='inner', left_on='geo', right_on='country')

df.drop(columns=['geo', 'country'], inplace=True)

df.rename(columns={
    'time': 'year', 
    'income_per_person_gdppercapita_ppp_inflation_adjusted': 'income', 
    'life_expectancy_years': 'lifespan', 
    'population_total': 'population', 
    'name': 'country', 
    'world_6region': 'region'
}, inplace=True)

df.shape
df.head()<jupyter_output><empty_output><jupyter_text>explore<jupyter_code>df.describe(exclude=[np.number])
# df.country.unique()
usa = df[df.country=='United States']
usa[usa.year.isin([1818, 1918, 2018])]<jupyter_output><empty_output><jupyter_text>plot<jupyter_code>now = df[df.year==2018]
print(now.shape)
now.sample(10)
sns.relplot(x='income', y='lifespan', hue='region', size='population', data=now);<jupyter_output><empty_output><jupyter_text>analyze outliers<jupyter_code>now[now.income > 80000]
entities[entities.name=='Macao, China'].T
qatar = now[now.country=='Qatar']

qatar_income = qatar.income.values[0]

qatar_lifespan = qatar.lifespan.values[0]
sns.relplot(x='income', y='lifespan', hue='region', size='population', data=now)

plt.text(x=qatar_income-5000, y=qatar_lifespan+1, s='Qatar')

plt.title('Qatar has the highest incomes in 2018');<jupyter_output><empty_output><jupyter_text>plot multiple years<jupyter_code>years = [1818, 1918, 2018]

centuries = df[df.year.isin(years)]

centuries[centuries.country=='Afghanistan'].head()
sns.relplot(x='income', y='lifespan', hue='region', size='population', col='year', data=centuries); 

plt.xscale('log');<jupyter_output><empty_output><jupyter_text>consistent countries per year?<jupyter_code>centuries.groupby('year').country.count()
centuries.groupby('country').year.count().head()<jupyter_output><empty_output><jupyter_text>point out a story<jupyter_code>years2 = [1918, 1938, 1958, 1978, 1998, 2018]

subset = df[df.year.isin(years2)]

sns.relplot(x='income', y='lifespan', hue='region', size='population', data=subset, row='year')

plt.xscale('log')
plt.xlim(150,150000)
for year in years2:
  sns.relplot(x='income', y='lifespan', hue='region', size='population', 
              data=df[df.year==year])
  
  plt.xscale('log')
  plt.ylim(20,90)
  plt.title(year)
  plt.axhline(y=50, color='grey')<jupyter_output><empty_output><jupyter_text>100 years ago few countries had lifespans *above* 50 years<jupyter_code>df[(df.year==1918) & (df.lifespan>=50)]<jupyter_output><empty_output><jupyter_text>today, no countries have lifespans *below* 50 years<jupyter_code>df[(df.year==2018) & (df.lifespan<=50)]<jupyter_output><empty_output><jupyter_text>interactive (colab)<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16660543717854517, 'fraction_numerical': 0.023328434974283614, 'mean_word_length': 6.868497109826589, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 48, 'https://': 14, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '602256', 'license': 'MIT', 'n_tokens_mistral': 2187, 'n_tokens_neox': 1979, 'n_words': 237, 'path': 'module4-sequence-your-narrative/LS_DS_124_Sequence_your_narrative.ipynb', 'repo_name': 'quinn-dougherty/DS-Sprint-02-Storytelling-With-Data', 'size': '5450'}