<jupyter_start><jupyter_text>ExampleElements of Data Scienceby [<NAME>](https://allendowney.com)[MIT License](https://opensource.org/licenses/MIT)<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Cocoa bean productionThis example is based on [this chart from Our World In Data](https://ourworldindata.org/grapher/cocoa-beans-production-by-region)The following cell downloads the data:<jupyter_code># Get the data file

import os

filename = 'cocoa-beans-production-by-region.csv'
if not os.path.exists(filename):
    !wget https://github.com/AllenDowney/ElementsOfDataScience/raw/master/data/cocoa-beans-production-by-region.csv<jupyter_output><empty_output><jupyter_text>Now we can read the data into a Pandas `DataFrame`.<jupyter_code>df = pd.read_csv(filename)
df.head()
df.rename(columns={'Crops - Cocoa, beans - 661 - Production - 5510 - tonnes': 'Tons'})
df.columns = ['Entity', 'Code', 'Year', 'Tons']
df.head()
df['MTons'] = df['Tons'] / 1e6
df['MTons'].describe()<jupyter_output><empty_output><jupyter_text>Here are the values in the `region` column.<jupyter_code>df['Entity'].value_counts()
recent = df['Year'] == 2018
is_country = df['Code'].notna()
df[recent & is_country].sort_values(by='MTons', ascending=False).head(10)<jupyter_output><empty_output><jupyter_text>A pivot table has four parts:1. Which variable goes down the rows?2. Which variable goes across the columns?3. Which variable are we summarizing?4. How are we summarizing (or aggregating it)?In this example, there is only one row for each Year/Country, so we don't really have to summarize.<jupyter_code>pivot = pd.pivot_table(df, index='Year', columns='Entity', values='MTons')
pivot.head()<jupyter_output><empty_output><jupyter_text>We can use Pandas to create a plot similar to the one in the article.<jupyter_code>regions = ['Africa', 'Asia', 'South America', 'Central America', 'Oceania']
subset = pivot[regions]<jupyter_output><empty_output><jupyter_text>The following function puts a label next to the data point for a given country.<jupyter_code>subset.plot()
plt.ylabel('Millions of Tons');
subset.plot(stacked=True)
plt.ylabel('Millions of Tons');
subset.plot(kind='area', stacked=True, alpha=0.5)
plt.ylabel('Millions of Tons');
countries = ["Cote d'Ivoire", 'Ghana', 'Indonesia', 'Nigeria', 'Cameroon', 'Togo']
subset = pivot[countries]
subset.plot()
plt.ylabel('Cocoa production (Millions of Tonnes)');
subset.plot(kind='area', stacked=True, alpha=0.5)
plt.ylabel('Cocoa production (Millions of Tonnes)');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14285714285714285, 'fraction_numerical': 0.008832565284178187, 'mean_word_length': 7.1152647975077885, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '118433', 'license': 'MIT', 'n_tokens_mistral': 926, 'n_tokens_neox': 882, 'n_words': 217, 'path': 'examples/cocoa_soln.ipynb', 'repo_name': 'itmeson/ElementsOfDataScience', 'size': '2610'}