{"cells":[{"metadata":{},"cell_type":"markdown","source":"# CDP Unlocking Climatic Solutions"},{"metadata":{"trusted":true},"cell_type":"code","source":"# import standard libs\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# import city response dataset\ndf_city_resp_18 = pd.read_csv('../input/cdp-unlocking-climate-solutions/Cities/Cities Responses/2018_Full_Cities_Dataset.csv')\n\n# import city disclosing dataset\ndf_city_disc_18 = pd.read_csv('../input/cdp-unlocking-climate-solutions/Cities/Cities Disclosing/2018_Cities_Disclosing_to_CDP.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Len of dataframe: \",len(df_city_disc_18))\nprint(\"Number of Account_number: \".upper(),len(df_city_disc_18['Account Number'].unique()))\nprint(\"Number of Organization: \".upper(),len(df_city_disc_18['Organization'].unique()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_city_disc_18.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Only population column of dataset has NULL values => replacing them by mean of population data"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_city_disc_18['Population'].fillna(df_city_disc_18['Population'].mean(), inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 4)\nplt.plot(df_city_disc_18['Population'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The histogram for population has a sharp peak, maybe caused due to error in entry, hence replacing that value by mean of data"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_city_disc_18['Population'][162] = df_city_disc_18['Population'].mean()\nplt.plot(df_city_disc_18['Population'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## City Responses Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_city_resp_18.sample(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_city_resp_18.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"size = df_city_resp_18['Parent Section'].value_counts().values\nplt.rcParams['figure.figsize'] = (7, 7)\nlabels2 = df_city_resp_18['Parent Section'].value_counts().keys()\nmy_circle = plt.Circle((0, 0), 0.7, color = 'white')\nplt.pie(size, labels = labels2, autopct = '%.2f%%')\nplt.title('Distribution of Parent Sections in Questionnaire', fontsize = 20)\nplt.legend(bbox_to_anchor=(2, 1), loc='upper left',)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## RELATION IN 2 TABLES: \nset_disc = set(df_city_disc_18['Account Number'])\nprint(\"Length of disclosing org: \", len(set_disc))\nset_resp = set(df_city_resp_18['Account Number'])\nprint(\"Length of Responses org: \", len(set_resp))\nset_uni = set_disc.union(set_resp)\nprint(\"Length of Union of 2 tables: \",len(set_uni))\nprint(\"Lenght of diff of disc and union: \", len(set_disc.difference(set_uni)))\nprint(\"==> Disclosing set has information of all organisations that are in responses file\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(25,7))\nY_org_count = list()\ngrp_org_count = df_city_resp_18.groupby('Country')\nfor name, group in grp_org_count:\n    Y_org_count.append(len(group['Organization'].unique()))\ntempdf = pd.DataFrame(list(zip(sorted(df_city_resp_18['Country'].unique()), Y_org_count)), columns = ['Country', 'count']) \ntempdf = tempdf.sort_values(by = ['count'], ascending=False)\nsns.barplot(x = tempdf['Country'], y = tempdf['count'])\nplt.xticks(rotation=90, fontsize = 20);\nplt.title('Number of Organisations per country', fontsize = 33);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Below chart depicts the distribution of 8 CDP Regions and percentage of data present for countries present within each region"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (6, 6)\ngrp_parent = df_city_resp_18.groupby('CDP Region')\n\nfor sect, group in grp_parent:\n    size = group['Country'].value_counts().values    \n    labels2 = group['Country'].value_counts().keys()\n    my_circle = plt.Circle((0, 0), 0.7, color = 'white')\n    plt.pie(size, labels = labels2, autopct = '%.2f%%')\n    plt.title('Distribution of {} '.format(sect), fontsize = 20)\n    p = plt.gcf()\n    p.gca().add_artist(my_circle)\n    plt.legend(bbox_to_anchor=(2, 1), loc='upper left')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_city_resp_18['Response Answer'].fillna(\"No Response\", inplace = True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Filling the NaN values in response column of city response dataset with \"No Response\""},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(25,6))\ndf2 = df_city_resp_18['Response Answer']\ndf_ha_ratio = df_city_resp_18[df2 != 'No Response'].groupby(['Country']).size()/df_city_resp_18.groupby(['Country']).size()\nax = sns.barplot(x=df_ha_ratio.index, y=df_ha_ratio)\nax.set_title(\"% of country with Response answers\", fontsize = 27)\nplt.xticks(rotation=90, fontsize = 17);\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Climate Hazards"},{"metadata":{},"cell_type":"markdown","source":"Climatic hazards are agents of disaster in terms of what they may do to human settlements or to the environment. Potentially hazardous atmospheric phenomena include tropical cyclones, thunderstorms, tornadoes, drought, rain, hail, snow, lightning, fog, wind, temperature extremes, air pollution, and climatic change. In the given dataset the information regarding climatic hazards that have already occured in the country is taken in account along with other intuitive factors and scales."},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Top 2 important questions asked with respect to climatic hazard:\".upper())\ndf_city_resp_18[df_city_resp_18['Section'] == 'Climate Hazards']['Question Name'].value_counts()[:2]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') & \n               (df_city_resp_18['Column Name'] == 'Climate Hazards')\n                      & (df_city_resp_18['CDP Region'] == 'North America')].copy()\n\nplt.rcParams['figure.figsize'] = (20, 12)\n\nax = sns.countplot(y = 'Response Answer', hue = 'Country', data = temp)\nax.set_title(\"Type of Climate Hazards\", fontsize = 27)\nplt.legend(loc = 'upper right', bbox_to_anchor = (1,1))\nplt.yticks(fontsize = 15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Above figure specifies the types of climatic hazards that have occured before or during 2018 in North America CDP Region as per the data"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 12)\nplt.subplot(2,2,1)\ntemp = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') & \n               (df_city_resp_18['Column Name'] == 'Probability of hazard')\n                      & (df_city_resp_18['CDP Region'] == 'North America')].copy()\nax = sns.countplot(x = 'Country', hue = 'Response Answer', data = temp)\nax.set_title(\"Probability of Hazard\", fontsize = 20)\nplt.legend(loc = 'upper left', fontsize = 15)\nplt.xticks(fontsize = 15)\n\nplt.subplot(2,2,2)\ntemp = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') & \n               (df_city_resp_18['Column Name'] == 'Future change in intensity')\n                      & (df_city_resp_18['CDP Region'] == 'North America')].copy()\nax = sns.countplot(x = 'Country', hue = 'Response Answer', data = temp)\nax.set_title(\"Future change in intensity\", fontsize = 20)\nplt.legend(loc = 'upper right', fontsize = 15)\nplt.xticks(fontsize = 15)\n\nplt.subplot(2,2,3)\ntemp = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') & \n               (df_city_resp_18['Column Name'] == 'Hazard status')\n                      & (df_city_resp_18['CDP Region'] == 'North America')].copy()\nax = sns.countplot(x = 'Country', hue = 'Response Answer', data = temp)\nax.set_title(\"Hazard status\", fontsize = 20)\nplt.legend(loc = 'upper right', fontsize = 15)\nplt.xticks(fontsize = 15)\n\nplt.subplot(2,2,4)\ntemp = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') & \n               (df_city_resp_18['Column Name'] == 'Magnitude of impact')\n                      & (df_city_resp_18['CDP Region'] == 'North America')].copy()\nax = sns.countplot(x = 'Country', hue = 'Response Answer', data = temp)\nax.set_title(\"Magnitude of impact\", fontsize = 20)\nplt.legend(loc = 'upper right', fontsize = 15)\nplt.xticks(fontsize = 15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"For each hazard faced by the the region, First and Third graph represents if the hazard that has already taken place is likely to hit the region again and with what intensity. While Second plot depicts the probability of hazard to occure in future. Fourth graph tells the intensity with which the hazard hit the region presently or in past. All the above information inferences to the 1st question in climate section."},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') & \n               (df_city_resp_18['Column Name'] == 'Top three assets/ services affected')\n                      & (df_city_resp_18['CDP Region'] == 'North America')].copy()\n\nplt.rcParams['figure.figsize'] = (20, 6)\n\nax = sns.countplot(x = 'Country', hue = 'Response Answer', data = temp, palette=sns.color_palette('Paired'))\nax.set_title(\"services affected\", fontsize = 27)\nplt.legend(bbox_to_anchor = (1.5, 1), loc = 'upper right', fontsize = 17)\nplt.xticks(fontsize = 20)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') & \n               (df_city_resp_18['Column Name'] == 'Anticipated timescale')\n                      & (df_city_resp_18['CDP Region'] == 'North America')].copy()\n\nplt.rcParams['figure.figsize'] = (10, 5)\n\nax = sns.countplot(x = 'Country', hue = 'Response Answer', data = temp)\nax.set_title(\"Anticipated timescale\", fontsize = 20)\nplt.legend(loc = 'upper right', fontsize = 15)\nplt.xticks(fontsize = 15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (29, 6)\n\ndf_ha_ratio = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') & \n               (df_city_resp_18['Column Name'] == 'Anticipated timescale') & \n               (df_city_resp_18['Response Answer'] == 'Short-term')].groupby(['CDP Region']).size()/df_city_resp_18.groupby(['CDP Region']).size()*100\n\nplt.subplot(1,3,1)\nax = sns.barplot(x=df_ha_ratio.index, y=df_ha_ratio,  palette=\"Paired\")\nax.set_title(\"% of Short-term timescale\", fontsize = 25)\nplt.xticks(rotation = 90, fontsize = 20)\nplt.yticks(fontsize = 15)\n\nplt.subplot(1,3,2)\ndf_ha_ratio = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') & \n               (df_city_resp_18['Column Name'] == 'Anticipated timescale') & \n               (df_city_resp_18['Response Answer'] == 'Long-term')].groupby(['CDP Region']).size()/df_city_resp_18.groupby(['CDP Region']).size()*100\nax = sns.barplot(x=df_ha_ratio.index, y=df_ha_ratio,  palette=\"Paired\")\nax.set_title(\"% of Long-term timescale\", fontsize = 25)\nplt.xticks(rotation = 90, fontsize = 20)\nplt.yticks(fontsize = 15)\n\nplt.subplot(1,3,3)\ndf_ha_ratio = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') & \n               (df_city_resp_18['Column Name'] == 'Anticipated timescale') & \n               (df_city_resp_18['Response Answer'] == 'Medium-term')].groupby(['CDP Region']).size()/df_city_resp_18.groupby(['CDP Region']).size()*100\nax = sns.barplot(x=df_ha_ratio.index, y=df_ha_ratio,  palette=\"Paired\")\nax.set_title(\"% of Medium-term timescale\", fontsize = 25)\nplt.xticks(rotation = 90, fontsize = 20)\nplt.yticks(fontsize = 15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Above graph tells the percentage of Short-term, Long-term and Medium-term Timescale for each CDP Region. Middle east is more prone to suffer from long term climatic hazard as compared to medium term hazard whereas Latin America is more prone to suffer from short term hazard as compared to long term hazard"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(25,10))\ntempX = df_city_resp_18[df_city_resp_18['Question Number'] == '2.4']['Response Answer'].value_counts()[:25]\ntempY = df_city_resp_18[df_city_resp_18['Question Number'] == '2.4']['Response Answer'].value_counts()[:25].index\nax = sns.barplot(x = tempX, y = tempY)\nax.set_title(\"Climate change vs Social Issues\", fontsize = 30)\nplt.xticks(fontsize = 15);\nplt.yticks(fontsize = 20)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = df_city_resp_18[(df_city_resp_18['Section'] == 'Energy') \n              & (df_city_resp_18['Row Name'] == 'Energy consumption percentage')\n                      & (df_city_resp_18['CDP Region'] == 'North America')].copy()\n\nplt.rcParams['figure.figsize'] = (20, 5)\ntemp = temp[temp['Response Answer'] != 'No Response']\ntemp = temp[temp['Response Answer'] != 'calculated field']\ntemp['Response Answer'] = pd.to_numeric(temp['Response Answer'], downcast = \"float\")\n\nax = sns.violinplot(x = 'Country', y = 'Response Answer', hue = 'Column Name', data = temp)\nplt.title(\"Energy consumption\", fontsize = 30, fontweight = 10)\nplt.legend(loc = 'upper right', fontsize = 17)\nplt.xticks(fontsize = 20)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = df_city_resp_18[(df_city_resp_18['Section'] == 'Energy') \n  & (df_city_resp_18['Column Name'] == 'Percentage renewable energy / electricity of total energy or electricity in base year')\n                      & (df_city_resp_18['CDP Region'] == 'North America')].copy()\n\nplt.rcParams['figure.figsize'] = (20, 5)\ntemp = temp[temp['Response Answer'] != 'No Response']\ntemp['Response Answer'] = pd.to_numeric(temp['Response Answer'], downcast = \"float\")\nax = sns.barplot(x = 'Country', y = 'Response Answer', data = temp, palette=sns.color_palette(\"Set2\"))\n\nax.set_title(\"% renewable energy / electricity of total energy or electricity in base year\", fontsize = 30)\nplt.xticks(fontsize = 15);\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# EMISSION"},{"metadata":{},"cell_type":"markdown","source":"Scope 1 covers direct emissions from owned or controlled sources. Scope 2 covers indirect emissions from the generation of purchased electricity, steam, heating and cooling consumed by the reporting company. Scope 3 includes all other indirect emissions that occur in a company's value chain.\n\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Breaking the city responses dataset into 2 parts\n# one carrying information about climate hazard and othe about scope 1 emission breakdown \n# and then merging the data on account number as common field\n\nt1 = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') \n       & (df_city_resp_18['Column Name'] == 'Climate Hazards')].copy()\nt2 = df_city_resp_18[(df_city_resp_18['CDP Region'] == 'North America')\n       & (df_city_resp_18['Section'] == 'Scope 1 Emissions Breakdown')].copy()\n\nsc1df = pd.merge(t2, t1[['Response Answer', 'Account Number']], on='Account Number', how='inner')\nsc1df.drop_duplicates(subset=None, keep=\"first\", inplace=True)\nsc1df.rename(columns={'Response Answer_y': 'type', 'Response Answer_x': 'Answer'}, inplace=True)\nsc1df.drop(['Questionnaire', 'Year Reported to CDP', 'Comments', 'File Name', 'Last update'], axis = 1, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Scope 1 Emissions Breakdown\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (24, 15)\ntemp = sc1df[ (sc1df['Answer'] != 'No Response')\n             & (sc1df['type'] != 'No Response')\n             & (sc1df['Column Name'] == 'Sector')].copy()\n\nax = sns.countplot(y = 'type', hue = 'Country', data = temp, palette=sns.color_palette(\"Paired\"))\nax.set_title(\" Type of hazard count for Scope 1 Emissions\", fontsize = 27)\nplt.yticks(fontsize = 20)\nplt.legend(loc = 'upper right', fontsize = 20)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (24, 17)\ntemp = sc1df[ (sc1df['Answer'] != 'No Response')\n             & (sc1df['type'] != 'No Response')\n             & (sc1df['Column Name'] == 'Source')].copy()\n\nax = sns.countplot(y = 'Answer', hue = 'Country', data = temp)\nax.set_title(\"Source + Scope 1 Emissions Breakdown\", fontsize = 27)\nplt.yticks(fontsize = 25)\nplt.legend(loc = 'upper right', fontsize = 25)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Source of Scope 1 emission for North America CDP Region, Buildings along with Municipal vehicle fleet is considered to be most important factors in scope 1 emissions"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (30, 22)\ntemp = sc1df[ (sc1df['Answer'] != 'No Response')\n             & (sc1df['type'] != 'No Response')\n             & (sc1df['Column Name'] == 'Fuel')].copy()\n\nax = sns.countplot(y = 'Answer', hue = 'Country', data = temp)\nax.set_title(\"Fuel + Scope 1 Emissions Breakdown\", fontsize = 27)\nplt.yticks( fontsize = 25)\nplt.legend(loc = 'upper right ', fontsize = 25)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Natural Gas, Compressed natural and Gas oil or Diesel are most common Fuel with scope 1 emissions"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (5, 5)\ntemp = sc1df[ (sc1df['Answer'] != 'No Response')\n             & (sc1df['Column Name'] == 'Amount')].copy()\ntemp['Answer'] = pd.to_numeric(temp['Answer'], downcast = \"float\")\nax = sns.barplot(x = 'Country', y = 'Answer', data = temp)\nax.set_title(\"Amount + Scope 1 Emissions Breakdown\", fontsize = 27)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (24, 20)\ntemp = sc1df[ (sc1df['Answer'] != 'No Response')\n             & (sc1df['Column Name'] == 'Amount')].copy()\n\ntemp['Answer'] = pd.to_numeric(temp['Answer'], downcast = \"float\")\nax = sns.barplot(y = 'type', x = 'Answer', hue = 'Country', data = temp)\nax.set_title(\" Amount of Scope 1 Emissions vs Climate hazard\", fontsize = 27)\nplt.yticks(fontsize = 25); plt.xticks(fontsize = 25)\nplt.legend(loc = 'upper right', fontsize = 25)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Regions suffering from climatic hazards as Fog, Extratropical storm and Monsoon have highest value/amount of scope 1 emission"},{"metadata":{},"cell_type":"markdown","source":"## Scope 2 Emissions Breakdown"},{"metadata":{"trusted":true},"cell_type":"code","source":"t1 = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') \n       & (df_city_resp_18['Column Name'] == 'Climate Hazards')].copy()\nt2 = df_city_resp_18[(df_city_resp_18['CDP Region'] == 'North America')\n       & (df_city_resp_18['Section'] == 'Scope 2 Emissions Breakdown')].copy()\n\nsc2df = pd.merge(t2, t1[['Response Answer', 'Account Number']], on='Account Number', how='inner')\nsc2df.drop_duplicates(subset=None, keep=\"first\", inplace=True)\nsc2df.rename(columns={'Response Answer_y': 'type', 'Response Answer_x': 'Answer'}, inplace=True)\nsc2df.drop(['Questionnaire', 'Year Reported to CDP', 'CDP Region', 'Comments', 'File Name', 'Last update'], axis = 1, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (24, 10)\ntemp = sc2df[ (sc2df['Answer'] != 'No Response')\n             & (sc2df['type'] != 'No Response')\n             & (sc2df['Column Name'] == 'Source')].copy()\n\nax = sns.countplot(x = 'Answer', hue = 'Country', data = temp, palette=sns.color_palette(\"Paired\"))\nax.set_title(\"Source of Scope 2 Emissions Breakdown\", fontsize = 27)\nplt.xticks(rotation = 90, fontsize = 25)\nplt.legend(loc = 'upper right', fontsize = 25)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Buildings, water supply and traffic signals are most likely to be cause of scope 2 emission in both USA and Canada while Thermal energy and Parking lot lighting are mainly responsible for scope 2 emission in Canada while Airports and municipal vehicle fleet are resonsible for emission in USA only"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (24, 10)\ntemp = sc2df[ (sc2df['Answer'] != 'No Response')\n             & (sc2df['type'] != 'No Response')\n             & (sc2df['Column Name'] == 'Type')].copy()\n\nax = sns.countplot(x = 'Answer', hue = 'Country', data = temp, palette=sns.color_palette(\"Paired\"))\nax.set_title(\"% Type Scope 2 Emissions count\", fontsize = 27)\nplt.xticks(rotation = 90, fontsize = 25)\nplt.legend(bbox_to_anchor = (1.05,1), fontsize = 25)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Electricity is a major reason of scope 2 emission, In Canada Cooling is more responsible for scope 2 emission"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (4, 4)\ntemp = sc2df[ (sc2df['Answer'] != 'No Response')\n             & (sc2df['Column Name'] == 'Amount')].copy()\ntemp['Answer'] = pd.to_numeric(temp['Answer'], downcast = \"float\")\nax = sns.barplot(x = 'Country', y = 'Answer', data = temp)\nax.set_title(\"Amount of Scope 2 Emissions Breakdown\", fontsize = 15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (24, 10)\ntemp = sc2df[ (sc2df['Answer'] != 'No Response')\n             & (sc2df['Column Name'] == 'Amount')].copy()\n\ntemp['Answer'] = pd.to_numeric(temp['Answer'], downcast = \"float\")\nax = sns.barplot(x = 'type', y = 'Answer', hue = 'Country', data = temp)\nax.set_title(\" Amount of Scope 2 Emissions vs type of hazard\", fontsize = 27)\nplt.xticks(rotation = 90, fontsize = 18)\nplt.legend(loc = 'upper right', fontsize = 25)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## GHG Emissions Data"},{"metadata":{},"cell_type":"markdown","source":"Greenhouse gases are those that absorb and emit infrared radiation in the wavelength range emitted by Earth. Carbon dioxide (0.04%), nitrous oxide, methane and ozone are trace gases that account for almost 0.1% of Earth's atmosphere and have an appreciable greenhouse effect."},{"metadata":{"trusted":true},"cell_type":"code","source":"t1 = df_city_resp_18[(df_city_resp_18['Section'] == 'Climate Hazards') \n       & (df_city_resp_18['Column Name'] == 'Climate Hazards')].copy()\nt2 = df_city_resp_18[(df_city_resp_18['CDP Region'] == 'North America')\n       & (df_city_resp_18['Section'] == 'GHG Emissions Data')].copy()\n\nghgdf = pd.merge(t2, t1[['Response Answer', 'Account Number']], on='Account Number', how='inner')\nghgdf.drop_duplicates(subset=None, keep=\"first\", inplace=True)\nghgdf.rename(columns={'Response Answer_y': 'type', 'Response Answer_x': 'Answer'}, inplace=True)\nghgdf.drop(['Questionnaire', 'Year Reported to CDP', 'CDP Region', 'Comments', 'File Name', 'Last update'], axis = 1, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (24, 20)\ntemp = ghgdf[ (ghgdf['Answer'] != 'No Response')\n             & (ghgdf['Column Name'] == 'Emissions (metric tonnes CO2e)')\n            & (ghgdf['Row Name'] == 'TOTAL BASIC emissions')].copy()\n\ntemp['Answer'] = pd.to_numeric(temp['Answer'], downcast = \"float\")\nax = sns.barplot(y = 'type', x = 'Answer', hue = 'Country', data = temp)\nax.set_title(\"% Type with TOTAL BASIC emissions\", fontsize = 27)\nplt.yticks(fontsize = 25)\nplt.legend(bbox_to_anchor = (1,1), fontsize = 25)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (24, 20)\ntemp = ghgdf[ (ghgdf['Answer'] != 'No Response')\n             & (ghgdf['Column Name'] == 'Emissions (metric tonnes CO2e)')\n            ].copy()\n\ntemp['Answer'] = pd.to_numeric(temp['Answer'], downcast = \"float\")\nax = sns.barplot(y = 'Row Name', x = 'Answer', hue = 'Country', data = temp)\nax.set_title(\"TOTAL BASIC emissions\", fontsize = 27)\nplt.yticks(fontsize = 23); plt.xticks(fontsize = 25)\nplt.legend(loc = 'upper right', fontsize = 25)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (5, 5)\ntemp = ghgdf[ (ghgdf['Answer'] != 'No Response')\n            & (ghgdf['Column Name'] == 'Source of Scope 3 emissions')].copy()\n\nax = sns.countplot(y = 'Answer', hue = 'Country', data = temp, palette=sns.color_palette(\"Paired\"))\nax.set_title(\"% Source of Scope 3 emissions\", fontsize = 27)\nplt.xticks(fontsize = 15)\nplt.legend(bbox_to_anchor = (1.05,1), fontsize = 15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# CORPORATIONS"},{"metadata":{"trusted":true},"cell_type":"code","source":"# import thr corporate dataset\ndf_corp = pd.read_csv('../input/cdp-unlocking-climate-solutions/Corporations/Corporations Disclosing/Climate Change/2018_Corporates_Disclosing_to_CDP_Climate_Change.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_corp.sample(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 10)\ntemp = df_corp.copy()\n\nax = sns.countplot(y = 'primary_industry', hue = 'country',  data = temp, palette=sns.color_palette(\"Paired\"))\nax.set_title(\"% primary_industry\", fontsize = 27)\nplt.yticks( fontsize = 16)\nplt.legend(bbox_to_anchor = (1.05,1), loc = 'upper right', fontsize = 17)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 17)\ntemp = df_corp.copy()\n\nax = sns.countplot(y = 'industries', hue = 'country',  data = temp, palette=sns.color_palette(\"Paired\"))\nax.set_title(\"% industries\", fontsize = 27)\nplt.yticks( fontsize = 16)\nplt.legend(loc = 'upper right', fontsize = 17)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 15)\ntemp = df_corp.copy()\n\nax = sns.countplot(y = 'primary_sector', hue = 'country',  data = temp)\nax.set_title(\"% primary_sector\", fontsize = 27)\nplt.yticks( fontsize = 16)\nplt.legend(loc = 'upper right', fontsize = 17)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Canada is more functional in Fossil Fuels, oil and gas processing, and metallic minerals mining"},{"metadata":{"trusted":true},"cell_type":"code","source":"# import corporate response dataset\n\ndf_corp_resp = pd.read_csv('../input/cdp-unlocking-climate-solutions/Corporations/Corporations Responses/Climate Change/2018_Full_Climate_Change_Dataset.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_corp_resp.sample(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_corp_resp['response_value'].fillna(\"No Response\", inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_corp_resp['module_name'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_corp_resp[df_corp_resp['module_name'] == 'C7. Emissions breakdowns']['column_name'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"a = set(list(df_corp['account_number']))\nb = set(list(df_corp_resp['account_number']))\nprint(len(a.intersection(b)), '(common entries in data)  --> can merge data')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"t1 = df_corp_resp[(df_corp_resp['module_name'] == 'C7. Emissions breakdowns')].copy()\n\ndf = pd.merge(t1, df_corp[[\"country\", 'primary_sector', 'primary_industry', 'primary_activity', 'account_number']], on='account_number', how='inner')\ndf.drop_duplicates(subset=None, keep=\"first\", inplace=True)\n\ndf.drop(['survey_year', 'response_received_date', 'accounting_period_to', 'ors_response_id', 'submission_date',\n            'page_name', 'module_name', 'row_number', 'comments',\n           'table_columns_unique_reference', 'data_point_name', 'data_point_id'], axis = 1, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"To determine the emission rates and amount from corporate dataset(emission information for each organisation), the response file is merged with corporate disclosing dataset(organization location wiht type of its industry and primary activities and sector information)."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 8)\ntemp = df[(df['response_value'] != 'No Response')\n            & (df['column_name'] == 'C7.2_C2Scope 1 emissions (metric tons CO2e)')].copy()\n\ntemp['response_value'] = pd.to_numeric(temp['response_value'], downcast = \"float\")\nax = sns.barplot(y = 'primary_industry', x = 'response_value', hue = 'country',  data = temp, palette=sns.color_palette(\"Set2\"))\nax.set_title(\"Scope 1 emissions (metric tons CO2e)\", fontsize = 25)\nplt.yticks (fontsize = 15)\nplt.legend(loc = 'upper right', fontsize = 17)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"According to corporate data, Scope 1 emissions are mainly generated where the industries are mainly of the type => Power generarion and infrastructure along wiht Transportation . This does matches to the city response data where scope 1 emission is found in large quantities for Buildins and municipal vehicle fleet releasing Fuel as oil gas and Diesel"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 10)\ntemp = df[(df['response_value'] != 'No Response')\n            & (df['column_name'] == 'C7.6b_C3Scope 2, market-based emissions (metric tons CO2e)')].copy()\n\ntemp['response_value'] = pd.to_numeric(temp['response_value'], downcast = \"float\")\nax = sns.barplot(y = 'primary_industry', x = 'response_value', hue = 'country',  data = temp, palette=sns.color_palette(\"Set2\"))\nax.set_title(\" Scope 2, market-based emissions (metric tons CO2e)\", fontsize = 25)\nplt.yticks(fontsize = 15)\nplt.legend(loc = 'upper right', fontsize = 20)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Scope 2 emission is mainly found in regions where there are industries mainly responsible for Fossil Fuels, Materials and reatil services as their primary activities."},{"metadata":{},"cell_type":"markdown","source":"#### The location-based method reveals what the company is physically putting into the air, and the market-based method shows emissions the company is responsible for through its purchasing decisions."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 15)\ntemp = df[(df['response_value'] != 'No Response')\n            & (df['column_name'] == 'C7.6b_C2Scope 2 location-based emissions (metric tons CO2e)')].copy()\n\ntemp['response_value'] = pd.to_numeric(temp['response_value'], downcast = \"float\")\nax = sns.barplot(y = 'primary_sector', x = 'response_value', hue = 'country',  data = temp, palette=sns.color_palette(\"Set2\"))\nax.set_title(\"Scope 2 location-based emissions (metric tons CO2e)\", fontsize = 27)\nplt.yticks(fontsize = 15)\nplt.legend(loc = 'upper right', fontsize = 20)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 7)\ntemp = df[(df['response_value'] != 'No Response')\n            & (df['column_name'] == 'C7.5_C4Purchased and consumed electricity, heat, steam or cooling (MWh)')].copy()\n\ntemp['response_value'] = pd.to_numeric(temp['response_value'], downcast = \"float\")\nax = sns.barplot(y = 'primary_industry', x = 'response_value', hue = 'country',  data = temp, palette=sns.color_palette(\"Set2\"))\nax.set_title(\"Purchased and consumed electricity, heat, steam or cooling (MWh)\", fontsize = 27)\nplt.yticks(fontsize = 15)\nplt.legend(loc = 'upper right', fontsize = 20)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Retail and Transportation industries require more amount of electricity."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 8)\ntemp = df[(df['response_value'] != 'No Response')\n            & (df['column_name'] == 'C7.5_C5Purchased and consumed low-carbon electricity, heat, steam or cooling accounted in market-based approach (MWh)')].copy()\n\ntemp['response_value'] = pd.to_numeric(temp['response_value'], downcast = \"float\")\nax = sns.barplot(y = 'primary_industry', x = 'response_value', hue = 'country',  data = temp, palette=sns.color_palette(\"Set2\"))\nax.set_title(\"Purchased and consumed low-carbon electricity, heat, steam or cooling accounted in market-based approach (MWh)\", fontsize = 27)\nplt.yticks(fontsize = 15)\nplt.legend(loc = 'upper right', fontsize = 20)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = (20, 5)\ntemp = df[(df['response_value'] != 'No Response')\n            & (df['column_name'] == 'C7.1a_C1Greenhouse gas')].copy()\n\ntemp = df[(df['response_value'] == 'CO2') | (df['response_value'] == 'CH4')\n            | (df['response_value'] == 'N2O') | (df['response_value'] == 'HFCs')\n         | (df['response_value'] == 'SF6') | (df['response_value'] == 'PFCs')\n          | (df['response_value'] == 'NF3')].copy()\n\nax = sns.countplot(y = 'response_value', hue = 'country',  data = temp, palette=sns.color_palette(\"Set2\"))\nax.set_title(\"Greenhouse gas\", fontsize = 15)\nplt.legend(loc = 'upper right', fontsize = 17)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}