Skip to content

Python

Libraries Used

import numpy as np
import pandas as pd

Data Loading

Mount Google Drive

from google.colab import drive
drive.mount('/contentdrive/')

Load Regions Table

# regions table
path='/contentdrive/MyDrive/SQL - Deforestation Porject/DB Files/regions.csv'
regions=pd.read_csv(path)
regions.head()
regions
Afghanistan
AFG
South Asia
Low income
Albania
ALB
Europe & Central Asia
Algeria
DZA
Middle East & North Africa
American Samoa
ASM
East Asia & Pacific
Andorra
AND
Europe & Central Asia
There are no rows in this table

Load Land Area Table

# land area table
path='/contentdrive/MyDrive/SQL - Deforestation Porject/DB Files/land_area.csv'
land_area=pd.read_csv(path)
land_area.head()
land_area
There are no rows in this table

Load Forest Area Table

# forest area table
path='/contentdrive/MyDrive/SQL - Deforestation Porject/DB Files/forest_area.csv'
forest_area=pd.read_csv(path)
forest_area.head()
forest area
There are no rows in this table

Data Processing

Merge Tables

# forsetation view table
forestation = regions.merge(land_area, on=["country_code"], how="inner")\
.merge(forest_area, on=["country_code", "year"], how="inner")

Convert Total Area to sq km

# Convert total area to sq km
forestation['total_area_sqkm'] = forestation['total_area_sq_mi'] * 2.59
# Remove total_area_sq_mi column
forestation.drop(columns=["total_area_sq_mi"], inplace=True)
# Remove nulls from the table
forestation = forestation.dropna(subset=["total_area_sqkm", "forest_area_sqkm"])
# Create a new column "forest_area_percent"
forestation['forest_area_percent'] = (forestation['forest_area_sqkm'] / forestation['total_area_sqkm'] * 100).round(2)

Display Forestation Data

forestation.head()
forestation
There are no rows in this table

Analysis

Total Forest Area in 1990

# Part 1 .a. What was the total forest area (in sq km) of the world in 1990?
# فلترة الجدول على 1990 & world
World_1990= forestation[(forestation['year'] == 1990) & (forestation['country_name'].str.lower() == 'world')]
# حساب إجمالي مساحة الغابات لعام 1990
total_forest_area_1990 = World_1990['forest_area_sqkm'].sum()
print(total_forest_area_1990)
Total Forest Area in 1990: 41282694.9 sq km

Total Forest Area in 2016

# Part 1 .b. What was the total forest area (in sq km) of the world in 2016?
# فلترة الجدول على 2016 & World
World_2016= forestation[(forestation['year'] == 2016) & (forestation['country_name'].str.lower() == 'world')]

# حساب إجمالي مساحة الغابات لعام 2016
total_forest_area_2016 = World_2016['forest_area_sqkm'].sum()
print(total_forest_area_2016)
Total Forest Area in 2016: 39958245.9 sq km

Change in Forest Area

# Part 1 .c.What was the change (in sq km) in the forest area of the world from 1990 to 2016?
# the change in the forest area from 1990 to 2016
change_in_forest_area = total_forest_area_1990 - total_forest_area_2016
print(change_in_forest_area)
Change in Forest Area: 1324449.0 sq km

Percent Change in Forest Area

# Part 1 .d. What was the percent change in forest area of the world between 1990 and 2016?
# the percent change in the forest area from 1990 to 2016
percent_change_in_forest_area = round((total_forest_area_1990 - total_forest_area_2016)/total_forest_area_1990 * 100,2)
print(percent_change_in_forest_area)
Percent Change in Forest Area: 3.21 %

Closest Country's Total Area to Forest Area Lost

# Part 1 .e. If you compare the amount of forest area lost between 1990 and 2016, to which country's total area in 2016 is it closest to?
# حساب مساحة كل دولة
forestation_2016 = forestation[(forestation['year'] == 2016) & (forestation['country_name'].str.lower() != 'world')]\
[['country_name','total_area_sqkm']].copy()
# حساب الفرق المطلق لكل دولة
forestation_2016['difference'] = abs(change_in_forest_area - forestation_2016['total_area_sqkm'])
# الدولة ذات الفرق الأقل
closest_country = forestation_2016.sort_values('difference',ascending=True).head(1)
print(closest_country)
Closest Country: Peru with a total area of 1,280,000 sq km.
country_name total_area_sqkm difference 4131 Peru 1.280000e+06 44449.0109

Regional Insights

Percent Forest Area by Region

# Part 2.(Regional Outlook) Create a table that shows the Regions and their percent forest area in 1990 and 2016.
# فلترة الجدول على عام 1990 وعام 2016
forestation_1990_2016 = forestation[forestation['year'].isin([1990, 2016])]

# تجميع البيانات حسب المنطقة والسنة
forestation_1990_2016 = forestation_1990_2016.groupby(['region', 'year'], as_index=False).agg(
total_land_area=('total_area_sqkm', 'sum'),
total_forest_area=('forest_area_sqkm', 'sum'))

# حساب نسبة الغابات
forestation_1990_2016['percent_forest_area'] = round((forestation_1990_2016['total_forest_area'] / forestation_1990_2016['total_land_area']) * 100,2)
# جدول يحتوي على region - year - percent forest area
r_insights=forestation_1990_2016[['region','year','percent_forest_area']]
print(r_insights)
r_insights
There are no rows in this table

the percent forest of the entire world in 2016

# Part 2 .a. What was the percent forest of the entire world in 2016?
World_2016 = r_insights[(r_insights['year'] == 2016) & (r_insights['region'].str.lower() == 'world')]
print(World_2016)
region year percent_forest_area World 2016 31.38

Highest Percent Forest in 2016

# Part 2 .a. What was the percent forest of the entire world in 2016?
World_2016 = r_insights[(r_insights['year'] == 2016) & (r_insights['region'].str.lower() == 'world')]
print(World_2016)
Highest Percent Forest Region in 2016: Latin America & Caribbean with 46.16%.
region year percent_forest_area 5 Latin America & Caribbean 2016 46.16

Lowest Percent Forest in 2016

# Part 2 .a. Which region had the HIGHEST percent forest in 2016?
region_2016 = r_insights[r_insights['year'] == 2016]
highest_region = region_2016.sort_values('percent_forest_area',ascending=False).head(1)
print(highest_region)
Lowest Percent Forest Region in 2016: Middle East & North Africa with 2.07%.
region year percent_forest_area 7 Middle East & North Africa 2016 2.07

the percent forest of the entire world in 1990

# Part 2 .b. What was the percent forest of the entire world in 1990?
World_1990 = r_insights[(r_insights['year'] == 1990) & (r_insights['region'].str.lower() == 'world')]
print(World_1990)
region year percent_forest_area 4 Latin America & Caribbean 1990 51.03

the LOWEST percent forest in 1990

# Part 2 .b.which region had the LOWEST percent forest in 1990?
region_1990 = r_insights[r_insights['year'] == 1990]
lowest_region = region_1990.sort_values('percent_forest_area',ascending=True).head(1)
print(lowest_region)
region year percent_forest_area 6 Middle East & North Africa 1990 1.78

the world DECREASED in forest area from 1990 to 2016

# Part 2 .c.which regions of the world DECREASED in forest area from 1990 to 2016?
# one table for 1990 / another table for 2016
region_1990 =r_insights[(r_insights['year'] == 1990) & (r_insights['region'] != 'World')]
region_2016 =r_insights[(r_insights['year'] == 2016) & (r_insights['region'] != 'World')]
# merge region 1990 & region 2016
merge_regions= region_1990.merge(region_2016, on=["region"], how="left", suffixes=('_1990', '_2016'))
# create a new column(pfa_decrease)
merge_regions['pfa_decrease'] = merge_regions['percent_forest_area_1990'] - merge_regions['percent_forest_area_2016']
# filter by pfa_decrease >0
result=merge_regions[merge_regions['pfa_decrease'] > 0]
print(result[['region','pfa_decrease']])
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.