我将编写一个函数而不是我的旧逐行代码,但看起来它不起作用。这段代码运行良好: def gdp_fdi(odf, colname): tempo = odf[odf['id'].str.contains(temp, na=False)] df = tempo[['id',colname]] return df def sortdf(df): df['id'] = pd.Categorical(df.id, categories = top20code, ordered = True) df.sort_values(by='id') return df<THIS PART> top20 =fdi.sort_values('fdi_1987',ascending = False).groupby('fdi_1987').head(2) top20 = top20[['fdi_1987','id']].head(21) top20code = top20['id'] top20code = top20code.to_string(index=False) top20code = top20code.split() temp = '|'.join(top20code) top20_name=gdp_fdi(wb,'name') top20_region=gdp_fdi(wb,'region') top20_gdp=gdp_fdi(gdp,'gdp_1987') sort20gdp=sortdf(top20_gdp) sort20region=sortdf(top20_region) sort20name=sortdf(top20_name) from functools import reduce lists=[sort20gdp,sort20region,sort20name] df = reduce(lambda df1, df2: pd.merge(left=df1, right=df2, on=["id"], how="inner"), lists) df['id'] = pd.Categorical(df.id, categories = top20code, ordered = True) df.sort_values(by='id') raw=df.sort_values(by='id') raw</THIS PART>但是当我把它写成一个功能,我用'fdi_'+str(year),而不是'fdi_1987'和写<THIS PART>作为命名功能top20(year)。但是当我运行这个功能时top20(1987),它说我有不可散列的类型:'list'我可以问为什么吗?重新设计功能的任何提示?
添加回答
举报
0/150
提交
取消