从文件Energy Indicators.xls加载能源数据,Energy Indicators.xls是联合国2013年能源供应和可再生电力生产的指标列表,应将其放入带有能源变量名称的DataFrame中
请记住,这是一个Excel文件,而不是逗号分隔的值文件。另外,请确保从数据文件中排除页脚和页眉信息。前两列是不必要的,因此您应该删除它们,并应更改列标签,以使这些列为:
[“国家”,“能源供应”,“人均能源供应”,“可再生能源百分比”]
将能源供应转换为千兆焦耳(在一千万焦耳中有1,000,000千兆焦耳)。对于所有缺少数据的国家/地区(例如带有“ ...”的数据),请确保将其反映为np.NaN值。
重命名以下国家/地区列表(用于以后的问题):
“大韩民国”:“韩国”, “美利坚合众国”:“美国”, “大不列颠及北爱尔兰联合王国”:“联合王国”, “中国香港特别行政区”:“香港”
也有几个国家/地区的名称中带有数字和/或括号。请确保将其删除,
例如
“玻利维亚(多民族国)”应为“玻利维亚”,
“ Switzerland17”应为“ Switzerland”。
接下来,从文件world_bank.csv中加载GDP数据,该文件是包含世界银行1960年至2015年国家GDP的csv。将此称为DataFrame GDP。
确保跳过标题,然后重命名以下国家/地区列表:
“韩国共和国”:“韩国”, “伊朗伊斯兰共和国”:“伊朗”, “中国香港特别行政区”:“香港”
最后,从文件scimagojr-3.xlsx加载能源工程和电力技术的Sciamgo期刊和国家排名数据,该文件根据国家在上述地区的期刊贡献来对国家进行排名。将此数据框称为ScimEn。
将三个数据集(GDP,能源和ScimEn)加入一个新的数据集(使用国名的交集)。仅使用最近10年(2006-2015年)的GDP数据,并且仅使用Scimagojr“排名”(排名1至15)排名前15位的国家。
此DataFrame的索引应为国家/地区的名称,列应为['Rank','Documents','Citabledocuments','Citations','Self-citations','每个文档的引文' ,“ H指数”,“能源供应”,“人均能源供应”,“可再生能源百分比”,“ 2006”,“ 2007”,“ 2008”,“ 2009”,“ 2010”,“ 2011”,“ 2012” ,“ 2013”,“ 2014”,“ 2015”]。
此函数应返回一个包含20列和15个条目的DataFrame。
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()
我几乎确信我的鳕鱼是真实的,但在线校正员说它是错误的 我在哪里弄错了?
答案 0 :(得分:0)
不是答案,只是一个简短的代码回顾:
x1_bounds >= 1.2*x2
语句应该是python文件的第一行。当然,它们绝对不应该在函数内部。
您的函数很长,请考虑将其分解为较小的函数。例如,一个函数来重命名未修饰的column / api / pandas.DataFrame.rename.html))
import
等
熊猫正在弃用energy = energy.rename({'Unnamed 1': 'Country', 'Unnamed 3':, 'Energy Supply'}, axis='columns)
,不要养成使用它的习惯
inplace
接受了一个dict,因此不需要循环。您可以一次完成所有操作,例如:.rename
rename(index={"United States of America": "United States", "Republic of Korea": "South Korea"}
,您需要num = list(string.digits)
。而且,我不知道您在import string
做什么,但是我确定您不应该这样做!
num
columns_to_keep = GDP.columns.str.contains('2006')
,x
,y
等不是可接受的变量名称。使用有意义的东西!
答案 1 :(得分:0)
您要返回的行数有问题。我在Coursera上完成了相同的作业,并评估了您的代码。您不应该将Comparing N to U, there were 1 rows where the values matched.
Comparing N to V, there were 1 rows where the values matched.
Comparing N to W, there were 1 rows where the values matched.
Comparing N to X, there were 1 rows where the values matched.
Comparing N to Y, there were 1 rows where the values matched.
Comparing N to Z, there were 1 rows where the values matched.
放在循环中,因为它们在循环外可以很好地工作。确保写重命名它们,然后合并。
df.rename()
答案 2 :(得分:0)
#能源
pandas
和 numpy
库lambda
函数将拍焦耳转换为千兆焦耳#GDP
#ScimEn
#Joining 三个数据集
merge()
加入最后,您将获得 15 行 20 列的数据框。
def answer_one():
# YOUR CODE HERE
import pandas as pd
import numpy as np
Energy=pd.read_excel('assets/Energy Indicators.xls')
Energy=pd.DataFrame(Energy)
Energy=Energy.drop(Energy.index[246:])
Energy=Energy.drop(Energy.index[:17])
Energy=Energy.dropna(how='all')
Energy=Energy.dropna(how='all',axis=1)
del Energy['Unnamed: 1']
Energy=Energy.rename(columns={'Unnamed: 2': 'Country','Unnamed: 3':'Energy Supply','Unnamed: 4':'Energy Supply per Capita','Unnamed: 5':'% Renewable'})
Energy=Energy.replace('...',np.nan)
Energy['Energy Supply']=Energy['Energy Supply'].apply(lambda x: x*1000000 )
Energy['Country']=Energy['Country'].replace(regex=True,to_replace='[^a-z A-Z]',value='')
Energy['Country']=Energy['Country'].replace(['Republic of Korea','United States of America','United Kingdom of Great Britain and Northern Ireland','China Hong Kong Special Administrative Region','Bolivia Plurinational State of','Falkland Islands Malvinas','Iran Islamic Republic of','China Macao Special Administrative Region','Micronesia Federated States of','Republic of Moldova','Sint Maarten (Dutch part)','State of Palestine','The former Yugoslav Republic of Macedonia','Venezuela Bolivarian Republic of','United Republic of Tanzania'],['South Korea','United States','United Kingdom','Hong Kong','Bolivia','Falkland Islands','Iran','Macao','Micronesia','Moldova','Sint Maarten','Palestine','Yugoslav','Venezuela','Tanzania'])
Energy=Energy.set_index('Country')
GDP=pd.read_csv('assets/world_bank.csv')
GDP=GDP.drop(GDP.index[:4])
GDP=GDP.drop(GDP.columns[4:50], axis=1)
GDP=GDP.rename(columns={'Data Source': 'Country','World Development Indicators':'Country Code',
'World Development Indicators':'Country Code',
'Unnamed: 2':'Indicator Name',
'Unnamed: 3':'Indicator Code',
'Unnamed: 50':'2006',
'Unnamed: 51':'2007',
'Unnamed: 52':'2008',
'Unnamed: 53':'2009',
'Unnamed: 54':'2010',
'Unnamed: 55':'2011',
'Unnamed: 56':'2012',
'Unnamed: 57':'2013',
'Unnamed: 58':'2014',
'Unnamed: 59':'2015'})
GDP['Country']=GDP['Country'].replace(['Korea, Rep.','Iran, Islamic Rep.','Hong Kong SAR, China'],['South Korea','Iran','Hong Kong'])
GDP=GDP.set_index('Country')
ScimEn=pd.read_excel('assets/scimagojr-3.xlsx')
ScimEn=ScimEn[ScimEn['Rank']<=15]
ScimEn.set_index('Country')
join_ScimEn_Energy=pd.merge(ScimEn,Energy, how='inner', on=['Country'])
Merged_all=pd.merge(join_ScimEn_Energy,GDP, how='inner', on=['Country'])
del Merged_all['Country Code']
del Merged_all['Indicator Name']
del Merged_all['Indicator Code']
Merged_all=Merged_all.set_index('Country')
return Merged_all
raise NotImplementedError()