Nhập môn Khoa học dữ liệu trong bài tập Python 3

Tải dữ liệu năng lượng từ tệp _______0_______4, là danh sách các chỉ số về cung cấp năng lượng và sản xuất điện tái tạo từ Liên Hợp Quốc cho năm 2013 và phải được đưa vào Khung dữ liệu với tên biến là năng lượng

Xin lưu ý rằng đây là tệp Excel chứ không phải tệp giá trị được phân tách bằng dấu phẩy. Ngoài ra, đảm bảo loại trừ thông tin chân trang và tiêu đề khỏi tệp dữ liệu. Hai cột đầu tiên không cần thiết, vì vậy bạn nên loại bỏ chúng và bạn nên thay đổi nhãn cột để các cột được

def answer_two():
    import pandas as pd
    import numpy as np

# 这些都是和Q1一样的在导入数据
    energy = pd.read_excel('Energy Indicators.xls', skiprows=17,skip_footer= 38)
    energy = energy[['Unnamed: 1','Petajoules','Gigajoules','%']]
    energy.columns = ['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']
    energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']] =  energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']].replace('...',np.NaN).apply(pd.to_numeric)
    energy['Energy Supply'] = energy['Energy Supply']*1000000
    energy['Country'] = energy['Country'].replace({'China, Hong Kong Special Administrative Region':'Hong Kong','United Kingdom of Great Britain and Northern Ireland':'United Kingdom','Republic of Korea':'South Korea','United States of America':'United States','Iran (Islamic Republic of)':'Iran'})
    energy['Country'] = energy['Country'].str.replace(" \(.*\)","")
    
    GDP = pd.read_csv('world_bank.csv',skiprows=4)
    GDP['Country Name'] = GDP['Country Name'].replace('Korea, Rep.','South Korea')
    GDP['Country Name'] = GDP['Country Name'].replace('Iran, Islamic Rep.','Iran')
    GDP['Country Name'] = GDP['Country Name'].replace('Hong Kong SAR, China','Hong Kong')

    columns = ['Country Name','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']
    
    GDP = GDP[columns]
    GDP.columns = ['Country','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']
    
    ScimEn = pd.read_excel('scimagojr-3.xlsx')
    ScimEn_m = ScimEn[:15]
    
    df = pd.merge(ScimEn, energy, how = 'inner', left_on = 'Country', right_on='Country')
    final_df = pd.merge(df,GDP, how = 'inner', left_on = 'Country', right_on='Country')
    final_df = final_df.set_index('Country')
    columns = ['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']
    ans = final_df[columns]

# 这里是使用otter方法合并数据  
    df2 = pd.merge(ScimEn, energy, how = 'outer', left_on = 'Country', right_on='Country')
    final_df2 = pd.merge(df2,GDP, how = 'outer', left_on = 'Country', right_on='Country')   
    # print(len(final_df2) - len(final_df))
    # 用outter合并的长度减去inner合并的长度,答案是156
    return len(final_df2) - len(final_df)

answer_two()
5

Chuyển đổi 

def answer_two():
    import pandas as pd
    import numpy as np

# 这些都是和Q1一样的在导入数据
    energy = pd.read_excel('Energy Indicators.xls', skiprows=17,skip_footer= 38)
    energy = energy[['Unnamed: 1','Petajoules','Gigajoules','%']]
    energy.columns = ['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']
    energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']] =  energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']].replace('...',np.NaN).apply(pd.to_numeric)
    energy['Energy Supply'] = energy['Energy Supply']*1000000
    energy['Country'] = energy['Country'].replace({'China, Hong Kong Special Administrative Region':'Hong Kong','United Kingdom of Great Britain and Northern Ireland':'United Kingdom','Republic of Korea':'South Korea','United States of America':'United States','Iran (Islamic Republic of)':'Iran'})
    energy['Country'] = energy['Country'].str.replace(" \(.*\)","")
    
    GDP = pd.read_csv('world_bank.csv',skiprows=4)
    GDP['Country Name'] = GDP['Country Name'].replace('Korea, Rep.','South Korea')
    GDP['Country Name'] = GDP['Country Name'].replace('Iran, Islamic Rep.','Iran')
    GDP['Country Name'] = GDP['Country Name'].replace('Hong Kong SAR, China','Hong Kong')

    columns = ['Country Name','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']
    
    GDP = GDP[columns]
    GDP.columns = ['Country','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']
    
    ScimEn = pd.read_excel('scimagojr-3.xlsx')
    ScimEn_m = ScimEn[:15]
    
    df = pd.merge(ScimEn, energy, how = 'inner', left_on = 'Country', right_on='Country')
    final_df = pd.merge(df,GDP, how = 'inner', left_on = 'Country', right_on='Country')
    final_df = final_df.set_index('Country')
    columns = ['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']
    ans = final_df[columns]

# 这里是使用otter方法合并数据  
    df2 = pd.merge(ScimEn, energy, how = 'outer', left_on = 'Country', right_on='Country')
    final_df2 = pd.merge(df2,GDP, how = 'outer', left_on = 'Country', right_on='Country')   
    # print(len(final_df2) - len(final_df))
    # 用outter合并的长度减去inner合并的长度,答案是156
    return len(final_df2) - len(final_df)

answer_two()
6 sang gigajoules (có 1.000.000 gigajoules trong một petajoule). Đối với tất cả các quốc gia có dữ liệu bị thiếu (e. g. dữ liệu với ". ") đảm bảo điều này được phản ánh dưới dạng giá trị _______0_______7

Đổi tên danh sách các quốc gia sau (để sử dụng trong các câu hỏi sau)

def answer_two():
    import pandas as pd
    import numpy as np

# 这些都是和Q1一样的在导入数据
    energy = pd.read_excel('Energy Indicators.xls', skiprows=17,skip_footer= 38)
    energy = energy[['Unnamed: 1','Petajoules','Gigajoules','%']]
    energy.columns = ['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']
    energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']] =  energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']].replace('...',np.NaN).apply(pd.to_numeric)
    energy['Energy Supply'] = energy['Energy Supply']*1000000
    energy['Country'] = energy['Country'].replace({'China, Hong Kong Special Administrative Region':'Hong Kong','United Kingdom of Great Britain and Northern Ireland':'United Kingdom','Republic of Korea':'South Korea','United States of America':'United States','Iran (Islamic Republic of)':'Iran'})
    energy['Country'] = energy['Country'].str.replace(" \(.*\)","")
    
    GDP = pd.read_csv('world_bank.csv',skiprows=4)
    GDP['Country Name'] = GDP['Country Name'].replace('Korea, Rep.','South Korea')
    GDP['Country Name'] = GDP['Country Name'].replace('Iran, Islamic Rep.','Iran')
    GDP['Country Name'] = GDP['Country Name'].replace('Hong Kong SAR, China','Hong Kong')

    columns = ['Country Name','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']
    
    GDP = GDP[columns]
    GDP.columns = ['Country','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']
    
    ScimEn = pd.read_excel('scimagojr-3.xlsx')
    ScimEn_m = ScimEn[:15]
    
    df = pd.merge(ScimEn, energy, how = 'inner', left_on = 'Country', right_on='Country')
    final_df = pd.merge(df,GDP, how = 'inner', left_on = 'Country', right_on='Country')
    final_df = final_df.set_index('Country')
    columns = ['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']
    ans = final_df[columns]

# 这里是使用otter方法合并数据  
    df2 = pd.merge(ScimEn, energy, how = 'outer', left_on = 'Country', right_on='Country')
    final_df2 = pd.merge(df2,GDP, how = 'outer', left_on = 'Country', right_on='Country')   
    # print(len(final_df2) - len(final_df))
    # 用outter合并的长度减去inner合并的长度,答案是156
    return len(final_df2) - len(final_df)

answer_two()
8

Ngoài ra còn có một số quốc gia có số và/hoặc dấu ngoặc đơn trong tên của họ. Hãy chắc chắn loại bỏ những thứ này,

e. g

def answer_two():
    import pandas as pd
    import numpy as np

# 这些都是和Q1一样的在导入数据
    energy = pd.read_excel('Energy Indicators.xls', skiprows=17,skip_footer= 38)
    energy = energy[['Unnamed: 1','Petajoules','Gigajoules','%']]
    energy.columns = ['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']
    energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']] =  energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']].replace('...',np.NaN).apply(pd.to_numeric)
    energy['Energy Supply'] = energy['Energy Supply']*1000000
    energy['Country'] = energy['Country'].replace({'China, Hong Kong Special Administrative Region':'Hong Kong','United Kingdom of Great Britain and Northern Ireland':'United Kingdom','Republic of Korea':'South Korea','United States of America':'United States','Iran (Islamic Republic of)':'Iran'})
    energy['Country'] = energy['Country'].str.replace(" \(.*\)","")
    
    GDP = pd.read_csv('world_bank.csv',skiprows=4)
    GDP['Country Name'] = GDP['Country Name'].replace('Korea, Rep.','South Korea')
    GDP['Country Name'] = GDP['Country Name'].replace('Iran, Islamic Rep.','Iran')
    GDP['Country Name'] = GDP['Country Name'].replace('Hong Kong SAR, China','Hong Kong')

    columns = ['Country Name','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']
    
    GDP = GDP[columns]
    GDP.columns = ['Country','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']
    
    ScimEn = pd.read_excel('scimagojr-3.xlsx')
    ScimEn_m = ScimEn[:15]
    
    df = pd.merge(ScimEn, energy, how = 'inner', left_on = 'Country', right_on='Country')
    final_df = pd.merge(df,GDP, how = 'inner', left_on = 'Country', right_on='Country')
    final_df = final_df.set_index('Country')
    columns = ['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']
    ans = final_df[columns]

# 这里是使用otter方法合并数据  
    df2 = pd.merge(ScimEn, energy, how = 'outer', left_on = 'Country', right_on='Country')
    final_df2 = pd.merge(df2,GDP, how = 'outer', left_on = 'Country', right_on='Country')   
    # print(len(final_df2) - len(final_df))
    # 用outter合并的长度减去inner合并的长度,答案是156
    return len(final_df2) - len(final_df)

answer_two()
9 phải là 
def answer_three():
    Top15 = answer_one()
    gdp = Top15[['2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']]
    avgGDP = gdp.mean(axis=1)
    return avgGDP
answer_three()
0,

def answer_three():
    Top15 = answer_one()
    gdp = Top15[['2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']]
    avgGDP = gdp.mean(axis=1)
    return avgGDP
answer_three()
1 nên là 
def answer_three():
    Top15 = answer_one()
    gdp = Top15[['2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']]
    avgGDP = gdp.mean(axis=1)
    return avgGDP
answer_three()
2

 

Tiếp theo, tải dữ liệu GDP từ tệp

def answer_three():
    Top15 = answer_one()
    gdp = Top15[['2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']]
    avgGDP = gdp.mean(axis=1)
    return avgGDP
answer_three()
3, là tệp csv chứa GDP của các quốc gia từ năm 1960 đến năm 2015 từ Ngân hàng Thế giới. Gọi khung dữ liệu này là GDP

Đảm bảo bỏ qua tiêu đề và đổi tên danh sách các quốc gia sau đây

def answer_three():
    Top15 = answer_one()
    gdp = Top15[['2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']]
    avgGDP = gdp.mean(axis=1)
    return avgGDP
answer_three()
4

 

Cuối cùng, tải dữ liệu Xếp hạng Quốc gia và Tạp chí Sciamgo về Kỹ thuật Năng lượng và Công nghệ Năng lượng từ tệp 

def answer_three():
    Top15 = answer_one()
    gdp = Top15[['2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']]
    avgGDP = gdp.mean(axis=1)
    return avgGDP
answer_three()
5, xếp hạng các quốc gia dựa trên đóng góp của tạp chí trong lĩnh vực nói trên. Gọi DataFrame này là ScimEn

Tham gia ba bộ dữ liệu. GDP, Năng lượng và ScimEn thành một bộ dữ liệu mới (sử dụng giao điểm của tên quốc gia). Chỉ sử dụng dữ liệu GDP trong 10 năm qua (2006-2015) và chỉ 15 quốc gia hàng đầu theo 'Xếp hạng' của Scimagojr (Xếp hạng 1 đến 15)

Chỉ mục của Khung dữ liệu này phải là tên của quốc gia và các cột phải là ['Xếp hạng', 'Tài liệu', 'Tài liệu có thể trích dẫn', 'Các trích dẫn', 'Tự trích dẫn', 'Các trích dẫn trên mỗi tài liệu', 'H

Hàm này sẽ trả về một Sê-ri 

def answer_four():
    Top15 = answer_one()
    avg = answer_three().sort_values(ascending=False)  # False指降序排列
    country = avg.index[5]
    value = Top15.loc[country]['2015'] - Top15.loc[country]['2006']
    return value

answer_four()
4 có chỉ số là tên quốc gia và có giá trị là chuỗi ước tính dân số