# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Enron Email Dataset Mini-Project
#
# Datasets and Questions Mini-Project
# ---
# The Enron fraud is a big, messy and totally fascinating story about corporate malfeasance of nearly every imaginable type. The Enron email and financial datasets are also big, messy treasure troves of information, which become much more useful once you know your way around them a bit. We’ve combined the email and finance data into a single dataset, which you’ll explore in this mini-project.

# Quizzes: Size of the Enron Dataset and Features in the Enron Dataset
# --
# The aggregated Enron email + financial dataset is stored in a dictionary, where each key in the dictionary is a person’s name and the value is a dictionary containing all the features of that person.
# The email + finance (E+F) data dictionary is stored as a pickle file, which is a handy way to store and load python objects directly. Use datasets_questions/explore_enron_data.py to load the dataset.
#
# How many data points (people) are in the dataset? And for each person, how many features are available?

# +
import pandas as pd

df = pd.DataFrame(pd.read_pickle('../final_project/final_project_dataset_unix2dos.pkl')).T
print('There\'re {} data points (i.e. people) and are avaiable {} features for each person.'.format(*df.shape))
# -

# After load the dataset, I've started exploring dataset features.

df.columns.tolist()

# Quiz: Finding POIs in the Enron Data
# --
# The "poi" feature records whether the person is a person of interest, according to our definition. 
#
# How many POIs are there in the E+F dataset?

num_pois = sum(df.poi==1)
print('There\'re {} POIs in the dataset.'.format(num_pois))

# Quiz: How Many POIs Exist?
# --
# We compiled a list of all POI names (in ../final_project/poi_names.txt) and associated email addresses (in ../final_project/poi_email_addresses.py).
#
# How many POI’s were there total?

# +
pois=pd.read_csv('../final_project/poi_names.txt')

print('There\'re {} POIs in total.'.format(len(pois)))
# -

# Quiz: Query the Dataset 1
# --
# What is the total value of the stock belonging to <NAME>?

print('The total value of the stock belonging to <NAME> is {}.'.format(df.loc['PRENTICE JAMES'].total_stock_value))

# Quiz: Query the Dataset 2
# --
# How many email messages do we have from <NAME> to persons of interest?

print('We have {} email messages from <NAME> to persons of interest.'.format(df.loc['COLWELL WESLEY'].from_this_person_to_poi))

# Quiz: Query the Dataset 3
# --
# What’s the value of stock options exercised by <NAME>?

print('The value of stock options exercised by <NAME> is equals to {}.'.format(df.loc['SKILLING JEFFREY K'].exercised_stock_options))

# Quiz: Follow the Money
# --
# Of these three individuals (Lay, Skilling and Fastow), who took home the most money (largest value of ``total_payments`` feature)?
#
# How much money did that person get?

total_payments = [df.loc['SKILLING JEFFREY K'].total_payments, \
                  df.loc['LAY KENNETH L'].total_payments, df.loc['FASTOW ANDREW S'].total_payments]
print('Jeffrey Skilling\'s total payments: {}'.format(df.loc['SKILLING JEFFREY K'].total_payments))
print('<NAME>\'s total payments: {}'.format(df.loc['LAY KENNETH L'].total_payments))
print('<NAME>\'s total payments: {}'.format(df.loc['FASTOW ANDREW S'].total_payments))
print('And clearly, <NAME> was who took home the most money.'.format(df.loc['LAY KENNETH L'].total_payments))

# Quiz: Dealing with Unfilled Features
# --
# How many folks in this dataset have a quantified salary? What about a known email address?

display(df[df.salary != 'NaN'].head())
display(df[df.email_address != 'NaN'].head())
print('{} folks in this dataset have a quantified salary and {} has a known email address.'\
      .format(len(df[df.salary != 'NaN']), len(df[df.email_address != 'NaN'])))

# Quiz: Missing POIs 1
# --
# How many people in the E+F dataset (as it currently exists) have “NaN” for their total payments? What percentage of people in the dataset as a whole is this?

display(df[df.total_payments == 'NaN'].head())
num_total_payments_nan = len(df[df.total_payments == 'NaN'])
print('{}% of people in the dataset have "NaN" for their payments.'\
     .format(round(num_total_payments_nan/len(df.total_payments)*100)))

# Quiz: Missing POIs 2
# --
# How many POIs in the E+F dataset have “NaN” for their total payments? What percentage of POI’s as a whole is this?

# +
total_payments_nan_and_poi = df[(df.total_payments == 'NaN') & (df.poi == True)]
display(total_payments_nan_and_poi)

num_total_payments_nan_and_poi = len(total_payments_nan_and_poi)
print('{}% of POIs in the E+F dataset have "NaN" for their total payments.'\
      .format(num_total_payments_nan_and_poi/len(df.total_payments)*100))
# -

# Quiz: Missing POIs 4
# --
# If you added in, say, 10 more data points which were all POI's, and put "NaN" for the total payments for those folks, the numbers you just calculated would change.
#
# What is the new number of people of the dataset? What is the new number of folks with "NaN" for total payments?

num_new_pois = 10
print('The dataset has now {} data points and the number of folks with "NaN" for total payments is equals to {}.'\
      .format(len(df) + num_new_pois, num_total_payments_nan + num_new_pois))

# Quiz: Missing POIs 5
# --
# What is the new number of POI’s in the dataset? What is the new number of POI’s with NaN for total_payments?

print('The new number of POI’s in the dataset is {} and the new number of POI\'s with NaN for total_payments is {}.'\
     .format(num_pois + num_new_pois, num_total_payments_nan_and_poi + num_new_pois))

StarcoderdataJupyterScriptsDedupFiltered