%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
#import seaborn as sns
#from scipy.stats import ks_2samp
#import glob
#import time
import requests
import bs4
#import lxml
import altair as alt
import folium
import geopandas as gdp
import math
import json
#from mpl_toolkits import mplot3d
import plotly
import plotly.express as px
import plotly.graph_objects as go
from ipywidgets import interact
#read the data
co2 = pd.read_csv("co2.csv")
temp = pd.read_csv("temperatures.csv")
crop = pd.read_csv("crops.csv")
pop = pd.read_excel('Population Estimates for Districts and PCs in India, 2020.xlsx')
geoinfo = pd.read_csv('district wise centroids.csv')
cs = pd.read_excel('cotton-180.xlsx',skiprows = 1)
India is highly vulnerable to climate change due to its considerable and growing population and growing economy, both closely tied to a limited natural resource base. More than half of the 1.3 billion population directly depend on climate sensitive sectors (agriculture, forestry, fisheries) and natural resources (water, biodiversity and coastlands) for subsistence and livelihood.[2] As India strives to develop its economy, rising industrialization, urbanization, agricultural commercialization, and expansion of power generation capacity based predominately on non-renewable fuels will rapidly increase green-house gas (GHG) emissions.
#scraping resource from[3]
sector = requests.get('https://statisticstimes.com/economy/country/india-gdp-sectorwise.php#:~:text=Sector%2Dwise%20GDP%20in%20India,Financial%2C%20real%20estate%20%26%20prof%20servs').text
sector = bs4.BeautifulSoup(sector)
tab = sector.find('table', attrs = {'id': 'table_id4'})
headers = []
for i in tab.find_all('th'):
headers.append(i.text)
head =[]
for h in headers[:7]:
head.append(h)
head.append(h+'1')
head = head[1:]
mydata = pd.DataFrame(columns = head)
for j in tab.find_all('tr')[2:]:
row_data = j.find_all('td')
row = [k.text for k in row_data]
mydata.loc[len(mydata)] = row
#slicing
mydata = mydata[['Year1','Agriculture & Allied', 'Agriculture',
'Industry', 'Mining & Quarrying', 'Manufacturing',
'Services']]
mydata['Year1'] = mydata['Year1'].str[:4].astype('int')
#Dataframe slicing,cleaning
mydata = mydata.sort_values(by = 'Year1', ascending = False).set_index('Year1')
value = [18.2, 15.42+ 19.53, 22.05, 2.7+ 5.16,12.89]
value.append(100-np.array(value).sum())
value.sort(reverse = False)
mydata.iloc[0]
Agriculture & Allied 18.20 Agriculture 15.79 Industry 24.77 Mining & Quarrying 2.13 Manufacturing 12.89 Services 57.03 Name: 2013, dtype: object
# Weighted Pie
source = pd.DataFrame({"category":
['Other','1.Construction', '2.Manufacturing', '3.Agriculture & Allied', '4.Financial, real-estate, etc', '5.Other servs'],"values": value})
base = alt.Chart(source).encode(
theta=alt.Theta("values:Q", stack=True),
radius=alt.Radius("values", scale=alt.Scale(type="sqrt", zero=True, rangeMin=20)),
color="category:N",
)
c1 = base.mark_arc(innerRadius=20, stroke="#fff")
c2 = base.mark_text(radiusOffset=10).encode(text="values:Q")
c1 + c2
About 60 to 88% of the human population depends on agriculture for their livelihoods, of which 12 to 93% of the people live in rainfed areas and 26 to 84% of the arable land.[1]
#Population dependency pie chart
source = pd.DataFrame(
{"category": ["Agriculture", "Industry", "Service"], "value": [70, 12 ,18]}
)
base = alt.Chart(source).encode(
theta=alt.Theta("value:Q", stack=True), color=alt.Color("category:N", legend=None)
)
pie = base.mark_arc(outerRadius=120)
text = base.mark_text(radius=150, size=12).encode(text="category:N")
pie + text
pop['District Name, State Letter Code'] = pop['District Name, State Letter Code'].str.split(',').apply(lambda x:x[0])
toge = pop.merge(geoinfo, left_on = 'District Name, State Letter Code', right_on = 'District', how = 'inner')
s_pop = pop.groupby('State Name').sum().reset_index()
s_pop.sort_values(by = 'District Population').head()
geodf = gdp.read_file('https://raw.githubusercontent.com/Subhash9325/GeoJson-Data-of-Indian-States/master/Indian_States')
s_pop['State Name'] = (s_pop['State Name'].replace('Andaman & Nicobar Islands','Andaman and Nicobar')
.replace('Jammu & Kashmir','Jammu and Kashmir')
.replace('NCT of Delhi','Delhi' )
.replace('Odisha', 'Orissa')
.replace('Uttarakhand', 'Uttaranchal')
.replace('Dadra and Nagar Haveli and Daman and Diu','Dadra and Nagar Haveli' ))
s_pop.head()
State Name | District Population | |
---|---|---|
0 | Andaman and Nicobar | 4.309542e+05 |
1 | Andhra Pradesh | 5.649770e+07 |
2 | Arunachal Pradesh | 1.592486e+06 |
3 | Assam | 3.561591e+07 |
4 | Bihar | 1.184479e+08 |
s_pop2 = s_pop.set_index('State Name')['District Population']
center_lat = toge['Latitude'].mean()
center_long = toge['Longitude'].mean()
m = folium.Map(location=[center_lat, center_long],tiles='cartodbpositron')#'CartoDB positron'
folium.FitBounds([(center_lat-15,center_long-19), (center_lat+12,center_long+19)]).add_to(m)
url = (
"https://raw.githubusercontent.com/Subhash9325/GeoJson-Data-of-Indian-States/master/Indian_States"
)
import branca
steps = s_pop2.to_numpy()
colorscale = branca.colormap.linear.YlOrBr_05.to_step(steps, np.arange(0,1,0.001)).scale(1e+04, 1.2e+08)
def style_function(feature):
popu = s_pop2.get((feature['properties']['NAME_1']), None)
return {
"fillOpacity": 0.6,
"weight": 0,
"fillColor": "#black" if popu is None else colorscale(popu),
}
folium.GeoJson(
json.loads(requests.get(url).text),
style_function=style_function,
).add_to(m)
m
#folium.TileLayer(tiles = 'MapQuest Open Aerial', attr = 'https://www.mapquest.com/').add_to(m)