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 »

View more »
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
169 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
April
January
February
March
April
May
June
July
August
September
October
November
December
2025
2025
2026
2027
SunMonTueWedThuFriSat
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
00:00
00:30
01:00
01:30
02:00
02:30
03:00
03:30
04:00
04:30
05:00
05:30
06:00
06:30
07:00
07:30
08:00
08:30
09:00
09:30
10:00
10:30
11:00
11:30
12:00
12:30
13:00
13:30
14:00
14:30
15:00
15:30
16:00
16:30
17:00
17:30
18:00
18:30
19:00
19:30
20:00
20:30
21:00
21:30
22:00
22:30
23:00
23:30