How to load an excel sheet and clean the data in python?
从文件energy indicators.xls中加载能源数据,该文件是联合国2013年能源供应和可再生电力生产指标的列表,并应放入具有可变能源名称的数据框中。
请记住,这是一个Excel文件,而不是逗号分隔的值文件。此外,请确保从数据文件中排除页脚和页眉信息。前两列是不必要的,因此您应该去掉它们,并更改列标签,使列:
【国家】、【能源供应】、【人均能源供应】、【可再生能源】把能源供应转换成千兆焦耳(一个小气压计有1000000千兆焦耳)。对于所有缺少数据的国家(例如,带有"…"的数据),确保这些数据反映为np.nan值。
重命名以下国家/地区列表(供以后的问题使用):"大韩民国":"韩国","美利坚合众国":"美利坚合众国","大不列颠及北爱尔兰联合王国":"联合王国","中国,香港特别行政区":"香港"
还有几个国家的名字中有数字和/或括号。一定要移除这些,例如"多民族玻利维亚国"应为"玻利维亚","switzerland17"应该是"瑞士"。
接下来,从文件worldu bank.csv中加载gdp数据,该文件是一个csv文件,其中包含世界银行1960年至2015年各国的gdp。称之为数据框架GDP。请确保跳过标题,并重命名以下国家/地区列表:"韩国,代表":"韩国","伊朗,伊斯兰共和国":"伊朗","香港特区,中国":"香港"
最后,从文件scimagojr-3.xlsx中加载SciamGo期刊和国家能源工程和电力技术排名数据,该文件根据各国在上述领域的期刊贡献对其进行排名。称这个数据帧为scimen。
将三个数据集(GDP、能源和科学)加入新的数据集(使用国家名称的交叉点)。仅使用过去10年(2006-2015年)的国内生产总值数据,仅使用Scimagojr"排名"前15名国家(排名1到15)。
该数据框架的索引应为国家名称,列应为[‘排名’、‘文件’、‘引文’、‘引文’、‘自述’、‘每份文件引文’、‘H索引’、‘能源供应’、‘人均能源供应’、‘可再生能源’、‘2006’、‘2007’、‘2008’、‘2009’、‘2010’、‘2011’、‘2012’、‘2013’、‘2014’、‘2015’]。
此函数应返回一个具有20列和15个条目的数据帧。
我尝试了以下代码来解决这个问题,但它只返回12行而不是15行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | import pandas as pd from pandas import ExcelWriter from pandas import ExcelFile pd.set_option('display.max_columns', None) pd.set_option('display.max_rows', None) Energy = pd.read_excel('Energy Indicators.xls') Energy.drop(Energy.columns[[0,1]],axis=1,inplace=True) Energy.columns=['Country','Energy Supply','Energy Supply per capita','% Renewable'] Energy['Energy Supply']*=1000000 Energy['Country'] = Energy['Country'].str.replace(r"\(.*\)","") Energy['Country'] = Energy['Country'].str.replace("[0-9()]+$","") Energy.replace('Republic of Korea','South Korea', inplace = True) Energy.replace('United States of America','United States', inplace = True) Energy.replace('United Kingdom of Great Britain and Northern Ireland','United Kingdom', inplace = True) Energy.replace('China, Hong Kong Special Administrative Region','Hong Kong', inplace = True) import pandas as pd GDP = pd.read_csv('world_bank.csv', index_col=0, header=None) GDP = GDP.drop(['Data Source']) GDP = GDP.dropna() GDP = GDP.reset_index() GDP.columns = GDP.iloc[0] GDP.drop(GDP.index[[0,3]], inplace=True) GDP = GDP.rename(columns={'Country Name': 'Country'}) GDP.replace(',','-', inplace=True) GDP = GDP.replace('Korea, Rep.','South Korea') GDP = GDP.replace('Iran, Islamic Rep.','Iran') GDP = GDP.replace('Hong Kong SAR, China','Hong Kong') import pandas as pd from pandas import ExcelWriter from pandas import ExcelFile pd.set_option('display.max_columns', None) pd.set_option('display.max_rows', None) ScimEn = pd.read_excel('scimagojr-3.xlsx') b = pd.merge(pd.merge(Energy,GDP,on='Country'),ScimEn,on='Country') a = pd.merge(pd.merge(Energy,GDP,on='Country'),ScimEn,on='Country') a = a.sort(['Rank'], ascending=[True]) a = a[a["Rank"] < 16] a=a.rename(columns = {'2006.0':'abc'}) a.columns.values[53] ="2006" a.columns.values[54] ="2007" a.columns.values[55] ="2008" a.columns.values[56] ="2009" a.columns.values[57] ="2010" a.columns.values[58] ="2011" a.columns.values[59] ="2012" a.columns.values[60] ="2013" a.columns.values[61] ="2014" a.columns.values[62] ="2015" a = a[['Country','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']] a = a.set_index('Country') def ans(): return a ans() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import numpy as np import pandas as pd def energy(): energy=pd.ExcelFile('Energy Indicators.xls').parse('Energy') energy=energy.iloc[16:243][['Environmental Indicators: Energy','Unnamed: 3','Unnamed: 4','Unnamed: 5']].copy() energy.columns=['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable'] energy = energy.replace('...', np.nan) energy['Energy Supply']=energy['Energy Supply']*1000000 energy = energy.replace("Republic of Korea","South Korea") energy = energy.replace("United States of America","United States") energy = energy.replace("United Kingdom of Great Britain and Northern Ireland","United Kingdom") energy = energy.replace("China, Hong Kong Special Administrative Region","Hong Kong") energy['Country'] = energy['Country'].str.extract('(^[a-zA-Z\s]+)', expand=False).str.strip() energy=energy.reset_index() energy=energy[['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']] return energy.iloc[43] def GDP(): GDP=pd.read_csv('world_bank.csv') s=(GDP.iloc[3].values)[:4].astype(str).tolist()+(GDP.iloc[3].values)[4:].astype(int).astype(str).tolist() GDP=GDP.iloc[4:] GDP.columns=s GDP=GDP[['Country Name','2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015']] GDP.columns=['Country','2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015'] GDP=GDP.replace("Korea, Rep.","South Korea",regex=False) GDP=GDP.replace("Iran, Islamic Rep.","Iran") GDP=GDP.replace("Hong Kong SAR, China","Hong Kong",regex=False) return GDP def ScimEn(): ScimEn=pd.ExcelFile('scimagojr-3.xlsx').parse('Sheet1') return ScimEn def result(): e= energy() G=GDP() S=ScimEn() tdf=pd.merge(e,G,on='Country') tdf=pd.merge(tdf,S,on='Country') res = tdf.sort_values(by=['Rank'], inplace = True) res = tdf.head(15) res=res.set_index('Country', inplace=False) return res |
请一个接一个地问你的问题。但是,我为您上面提出的一些问题提出了解决方案。可以使用
1 | en = ex.parse('sheetname', skiprows = 2, skip_footer =True,..) |
然后,可以使用以下语法替换特殊字符
1 | en.replace('$%^',np.NaN, inplace =True) |
现在,您可以检查数据帧的头,查看并检查还有多少行需要跳过,以及其他详细信息。
要删除列,可以使用以下语法
1 | en.drop([dol for col in ['colname1', 'colname2', ...] if col in en], axis =1, inplace =True) |
从大问题来看,这是暂时的。处理这个问题,如果这样做,make将作为读取Excel文件、跳过行、替换NaN和跳过列的部分的答案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | def answer_one(): import pandas as pd energy=pd.read_excel('Energy Indicators.xls', skiprows=2) energy.columns=['a','b','Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable'] del energy['a'] del energy['b'] energy['Energy Supply']*=1000000 energy['Country'] = energy['Country'].str.replace(r"\(.*\)","") energy['Country'] = energy['Country'].str.replace("[0-9()]+$","") energy.replace('Republic of Korea','South Korea', inplace = True) energy.replace('United States of America','United States', inplace = True) energy.replace('United Kingdom of Great Britain and Northern Ireland','United Kingdom', inplace = True) energy.replace('China, Hong Kong Special Administrative Region','Hong Kong', inplace = True) GDP=pd.read_csv('world_bank.csv',skiprows=4) GDP.replace('Korea, Rep.','South Korea') GDP.replace('Iran, Islamic Rep.','Iran') GDP.replace('Hong Kong SAR, China' , 'Hong Kong') ScimEn=pd.read_excel('scimagojr-3.xlsx') GDP.columns=['Country', 'Country Code', 'Indicator Name', 'Indicator Code', '1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015'] for i in ['1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005']: del GDP[i] ScimEn=ScimEn[ScimEn['Rank']<16] x=pd.merge(GDP,ScimEn,how='inner',left_on='Country',right_on='Country') y=pd.merge(x,energy,how='inner',left_on='Country',right_on='Country') y=y.set_index('Country') del y['Country Code'] del y['Indicator Name'] del y['Indicator Code'] return y answer_one() |