a)问题a需要读取sheet2的数据,利用pandas统计stockpile不为0的country数目
import pandas as pd
#task1-a
data=pd.read_excel('2022_APMCM_E_Data.xlsx',sheet_name='stockpiles')
data1=data[data['Stockpile']>0]
print(data1['Country'].value_counts())结果:列出的国家即是答案
b)计算2002-2022年个国家的涨幅/跌幅
listcountries=data['Country'].unique().tolist()#列表保存涉及的国家
range=[]
for c in listcountries:
r=data[(data['Country']==c)&(data['Year']==2022)]['Stockpile'].values-data[(data['Country']==c)&(data['Year']==2002)]['Stockpile'].values
range.append(r)
task1b=pd.DataFrame({'Country':listcountries,'Range':range})
print(task1b)
print(task1b['Range'].abs().max()) #1-b