Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AI and Deep Learning by (17.6k points)

Load the energy data from the file Energy Indicators.xls, which is a list of indicators of energy supply and renewable electricity production from the United Nations for the year 2013, and should be put into a DataFrame with the variable name of energy.

Keep in mind that this is an Excel file, and not a comma separated values file. Also, make sure to exclude the footer and header information from the datafile. The first two columns are unneccessary, so you should get rid of them, and you should change the column labels so that the columns are:

['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']

Convert Energy Supply to gigajoules (there are 1,000,000 gigajoules in a petajoule). For all countries which have missing data (e.g. data with "...") make sure this is reflected as np.NaN values.

Rename the following list of countries (for use in later questions):

"Republic of Korea": "South Korea", "United States of America": "United States", "United Kingdom of Great Britain and Northern Ireland": "United Kingdom", "China, Hong Kong Special Administrative Region": "Hong Kong"

There are also several countries with numbers and/or parenthesis in their name. Be sure to remove these,

e.g.

'Bolivia (Plurinational State of)' should be 'Bolivia',

'Switzerland17' should be 'Switzerland'.

Next, load the GDP data from the file world_bank.csv, which is a csv containing countries' GDP from 1960 to 2015 from World Bank. Call this DataFrame GDP.

Make sure to skip the header, and rename the following list of countries:

"Korea, Rep.": "South Korea", "Iran, Islamic Rep.": "Iran", "Hong Kong SAR, China": "Hong Kong"

Finally, load the Sciamgo Journal and Country Rank data for Energy Engineering and Power Technology from the file scimagojr-3.xlsx, which ranks countries based on their journal contributions in the aforementioned area. Call this DataFrame ScimEn.

Join the three datasets: GDP, Energy, and ScimEn into a new dataset (using the intersection of country names). Use only the last 10 years (2006-2015) of GDP data and only the top 15 countries by Scimagojr 'Rank' (Rank 1 through 15).

The index of this DataFrame should be the name of the country, and the columns should be ['Rank', 'Documents', 'Citable documents', 'Citations', 'Self-citations', 'Citations per document', 'H index', 'Energy Supply', 'Energy Supply per Capita', '% Renewable', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015'].

This function should return a DataFrame with 20 columns and 15 entries.

def answer_one():
    import pandas as pd
    import numpy as np
    energy = pd.read_excel('Energy+Indicators.xls').drop(
        ['Environmental Indicators: Energy'],axis=1).dropna(axis=1,how='all'
                                                           ).dropna(axis=0,how='any')
    for col in energy.columns:
        if col=='Unnamed: 1':
            energy.rename(columns={col:'Country'}, inplace=True)
        if col=='Unnamed: 3':
            energy.rename(columns={col:'Energy Supply'}, inplace=True)
        if col=='Unnamed: 4':
            energy.rename(columns={col:'Energy Supply per Capita'}, inplace=True)
        if col=='Unnamed: 5':
            energy.rename(columns={col:'% Renewable'}, inplace=True)
    energy.replace( '...',np.nan, inplace=True)
    energy['Energy Supply'] = energy['Energy Supply'].apply(lambda x: x*1000000)
    energy.set_index('Country',inplace=True)
    num=['0','1','2','3','4','5','6','7','8','9']
    for idx in energy.index:
        if idx=="Republic of Korea":
             energy.rename(index={idx:"South Korea"}, inplace=True)
        if idx=="United States of America":
             energy.rename(index={idx:"United States"}, inplace=True)
        if idx=="United Kingdom of Great Britain and Northern Ireland":
             energy.rename(index={idx:"United Kingdom"}, inplace=True)
        if idx=="China, Hong Kong Special Administrative Region":
             energy.rename(index={idx:"Hong Kong"}, inplace=True)
        i=0
        for x in idx:
            if idx[i]=='(':
                energy.rename(index={idx:idx[:i-1]}, inplace=True)
            if (idx[i] in num):
                energy.rename(index={idx:idx[:i]}, inplace=True)
            i+=1
    GDP=pd.read_csv('world_bank.csv',index_col=0,skiprows=4)
    for idx in GDP.index:
        if idx=="Korea, Rep.":
            GDP.rename(index={idx:"South Korea"}, inplace=True)
        if idx=="Iran, Islamic Rep.":
            GDP.rename(index={idx:"Iran"}, inplace=True)
        if idx=="Hong Kong SAR, China":
            GDP.rename(index={idx:"Hong Kong"}, inplace=True)
    ScimEn=pd.read_excel('scimagojr-3.xlsx')
    columns_to_keep=[]
    for i in range(10):
        columns_to_keep.append(str(2006+i))
    GDP=GDP[columns_to_keep]
    ScimEn=ScimEn.where(ScimEn['Rank']<16).dropna(axis=0,how='all')
    GDP.rename_axis('Country',inplace=True)
    ScimEn.set_index('Country',inplace=True)
    x=pd.merge(ScimEn,energy, how='inner', left_index=True, right_index=True)
    y=pd.merge(x,GDP,how='inner', left_index=True, right_index=True)
    return y
answer_one()
i almost confident that my cod is true but the online corrector say it is false where did i do mistake?

1 Answer

0 votes
by (41.4k points)

Steps to follow while writing code:

1.First line of code should consist of import for python file.

2.Break down the functions into smaller functions.

3.energy = energy.rename({'Unnamed 1': 'Country', 'Unnamed 3':, 'Energy Supply'}, axis='columns) etc

4.You are using loop unnecessarily because rename takes a dict. 

 You can do it in a simple way.:

rename(index={"United States of America": "United States", "Republic of Korea": "South Korea"}

5.num = list(string.digits), so, you should import string

6.Use columns_to_keep = GDP.columns.str.contains('2006')

7.Also, x, y, i etc are not admissible variable names.

Browse Categories

...