Homework Project #4: Using Pivot Tables Objective: To apply knowledge about Pivot Tables to analyze data. Dataset: For this assignment, you will use the happiness dataset that contains panel data with...

1 answer below »
Instructions for homework highlighted on the document. Please refer to the slides for the python code and use Jupyter Notebook.


Homework Project #4: Using Pivot Tables Objective: To apply knowledge about Pivot Tables to analyze data. Dataset: For this assignment, you will use the happiness dataset that contains panel data with country and years related to the happiness index. The dataset is described in Appendix 1. Requirements: Using the variable assigned “freedom to make life choices” & your Python code should do the following (See sample output on Appendix 2). 1. Reduce your dataset to the last four years (2017-2020) and keep all the variables. Add code to answer how many rows and columns are in the reduced dataset, and whether your variable has any missing values. 2. Calculate the average of your assigned variable for all countries across the four-year period. Sort the dataset to show first the countries with the highest values. 3. Calculate the median of your assigned variable by region. 4. Calculate the mean of the variable by region and year and graphically show how the variable has changed. Code Documentation: The first line of your Python code should include a comment with your name, course section and date. You should also include other comments in your program explaining your code, variable names, or approaches. Submit for grading: For this assignment, you need to upload two files: a plain text file (.txt) with your documented Python code, and a Word document (.doc) with screenshots of Jupyter notebook runs and results. For this assignment, you need to write a paragraph in your Word document analyzing this variable based on the results of your tables (#2-#4 above). Grading Rubric: 20% independent verification of program run; 50% required and correct output; 20% file submission compliance; 10% authorship, code, and printout documentation. Appendix 1: Data Set Description Six key variables are combined to form the happiness score index calculated for each country: · Economy: Log GDP per capita · Family: social support · Health: healthy life expectancy at birth · Freedom: freedom to make life choices · Generosity: perceptions of generosity · Trust: perceptions of corruption The “Life Ladder” is the main life evaluation question, where 10 is the best possible life and 0 is the worst. You can read more about this at: https://worldhappiness.report/ed/2021/ Appendix 2: Sample Output (for the Life Ladder Variable) Q1: Characteristics of reduced dataset and missing values on Life Ladder Q2: Average by country with sorted results Q3: Median by Region and Year Q4: Mean by Region and Year in Table and Graph
Answered 1 days AfterApr 27, 2021

Answer To: Homework Project #4: Using Pivot Tables Objective: To apply knowledge about Pivot Tables to analyze...

Sanchi answered on Apr 28 2021
167 Votes
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#read dataset\n",
"data = pd.read_csv('whrdata-gzf3sjol.csv')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"d>50.80\n",
"
\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"
Country nameyearRegionLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruption
0Afghanistan2008Southern Asia3.7247.3700.4510.7180.1680.882
1Afghanistan2009Southern Asia4.4027.5400.55251.200.6790.1900.850
2Afghanistan2010Southern Asia4.7587.6470.53951.600.6000.1210.707
3Afghanistan2011Southern Asia3.8327.6200.52151.920.4960.1620.731
4Afghanistan2012Southern Asia3.7837.7050.52152.240.5310.2360.776
.................................
1944Zimbabwe2016Sub-Saharan Africa3.7357.9840.76854.400.733-0.0950.724
1945Zimbabwe2017Sub-Saharan Africa3.6388.0160.75455.000.753-0.0980.751
1946Zimbabwe2018Sub-Saharan Africa3.6168.0490.77555.600.763-0.0680.844
1947Zimbabwe2019Sub-Saharan Africa2.6947.9500.75956.200.632-0.0640.831
1948Zimbabwe2020Sub-Saharan Africa3.1607.8290.71756.800.643-0.0090.789
\n",
"

1949 rows × 10 columns

\n",
"
"
],
"text/plain": [
" Country name year Region Life Ladder Log GDP per capita \\\n",
"0 Afghanistan 2008 Southern Asia 3.724 7.370 \n",
"1 Afghanistan 2009 Southern Asia 4.402 7.540 \n",
"2 Afghanistan 2010 Southern Asia 4.758 7.647 \n",
"3 Afghanistan 2011 Southern Asia 3.832 7.620 \n",
"4 Afghanistan 2012 Southern Asia 3.783 7.705 \n",
"... ... ... ... ... ... \n",
"1944 Zimbabwe 2016 Sub-Saharan Africa 3.735 7.984 \n",
"1945 Zimbabwe 2017 Sub-Saharan Africa 3.638 8.016 \n",
"1946 Zimbabwe 2018 Sub-Saharan Africa 3.616 8.049 \n",
"1947 Zimbabwe 2019 Sub-Saharan Africa 2.694 7.950 \n",
"1948 Zimbabwe 2020 Sub-Saharan Africa 3.160 7.829 \n",
"\n",
" Social support Healthy life expectancy at birth \\\n",
"0 0.451 50.80 \n",
"1 0.552 51.20 \n",
"2 0.539 51.60 \n",
"3 0.521 51.92 \n",
"4 0.521 52.24 \n",
"... ... ... \n",
"1944 0.768 54.40 \n",
"1945 0.754 55.00 \n",
"1946 0.775 55.60 \n",
"1947 0.759 56.20 \n",
"1948 0.717 56.80 \n",
"\n",
" Freedom to make life choices Generosity Perceptions of corruption \n",
"0 0.718 0.168 0.882 \n",
"1 0.679 0.190 0.850 \n",
"2 0.600 0.121 0.707 \n",
"3 0.496 0.162 0.731 \n",
"4 0.531 0.236 0.776 \n",
"... ... ... ... \n",
"1944 0.733 -0.095 0.724 \n",
"1945 0.753 -0.098 0.751 \n",
"1946 0.763 -0.068 0.844 \n",
"1947 0.632 -0.064 0.831 \n",
"1948 0.643 -0.009 0.789 \n",
"\n",
"[1949 rows x 10 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['Country name', 'year', 'Region', 'Life Ladder', 'Log GDP per capita',\n",
" 'Social support', 'Healthy life expectancy at birth',\n",
" 'Freedom to make life choices', 'Generosity',\n",
" 'Perceptions of corruption'],\n",
" dtype='object')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#name of the columns\n",
"data.columns"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"#List of years to keep\n",
"years_keep = [2017,2018,2019,2020]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"
Country nameyearRegionLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruption
9Afghanistan2017Southern Asia2.6627.6970.49152.80.427-0.1210.954
10Afghanistan2018Southern Asia2.6947.6920.50852.60.374-0.0940.928
11Afghanistan2019Southern Asia2.3757.6970.42052.40.394-0.1080.924
21Albania2017Central and Eastern Europe4.6409.4760.63868.40.750-0.0290.876
22Albania2018Central and Eastern Europe5.0049.5180.68468.70.8240.0090.899
.................................
1933Zambia2020Sub-Saharan Africa4.8388.1170.76756.30.7500.0560.810
1945Zimbabwe2017Sub-Saharan Africa3.6388.0160.75455.00.753-0.0980.751
1946Zimbabwe2018Sub-Saharan Africa3.6168.0490.77555.60.763-0.0680.844
1947Zimbabwe2019Sub-Saharan Africa2.6947.9500.75956.20.632-0.0640.831
1948Zimbabwe2020Sub-Saharan Africa3.1607.8290.71756.80.643-0.0090.789
\n",
"

528 rows × 10 columns

\n",
"
"
],
"text/plain": [
" Country name year Region Life Ladder \\\n",
"9 Afghanistan 2017 Southern Asia 2.662 \n",
"10 Afghanistan 2018 Southern Asia 2.694 \n",
"11 Afghanistan 2019 Southern Asia 2.375 \n",
"21 Albania 2017 Central and Eastern Europe 4.640 \n",
"22 Albania 2018 Central and Eastern Europe 5.004 \n",
"... ... ... ... ... \n",
"1933 Zambia 2020 Sub-Saharan Africa 4.838 \n",
"1945 Zimbabwe 2017 Sub-Saharan Africa 3.638 \n",
"1946 Zimbabwe 2018 Sub-Saharan Africa 3.616 \n",
"1947 Zimbabwe 2019 Sub-Saharan Africa 2.694 \n",
"1948 Zimbabwe 2020 Sub-Saharan Africa 3.160 \n",
"\n",
" Log GDP per capita Social support Healthy life expectancy at birth \\\n",
"9 7.697 0.491 52.8 \n",
"10 7.692 0.508 52.6 \n",
"11 7.697 0.420 52.4 \n",
"21 9.476 0.638 68.4 \n",
"22 9.518 0.684 68.7 \n",
"... ... ... ... \n",
"1933 8.117 0.767 56.3 \n",
"1945 8.016 0.754 55.0 \n",
"1946 8.049 0.775 55.6 \n",
"1947 7.950 0.759 56.2 \n",
"1948 7.829 0.717 56.8 \n",
"\n",
" Freedom to make life choices Generosity...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here