unhashable type: 'list' while in function, but works well outside the function : <p>I am going to write a function instead of my old line by line code but looks like it doesn't work.</p>

<p>This piece of  code works well:</p>

<pre><code>    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
&lt;THIS PART&gt;
    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
&lt;/THIS PART&gt;
</code></pre>

<p>but when I write it as a function, I use <code>'fdi_'+str(year)</code> instead of <code>'fdi_1987'</code> and write <code>&lt;THIS PART&gt;</code> as a function named <code>top20(year)</code>.</p>

<p>But when I run the fuction by <code>top20(1987)</code>, it sais I have </p>

<blockquote>
  <p>unhashable type: 'list'</p>
</blockquote>

<p>May I ask why? any tips for redesign the function?</p>
