<p>对于me secons解决方案,效果很好-嵌套键中唯一必需的数字是字符串:</p>
<pre><code>d_dict = {'Status' : { '4':'Married', '5':'UnMarried'},
'Country': { '2': 'Japan' , '3': 'China'},
'Income': {'3': "5000-10000", '2': "11000-20000"}}
df = df.astype(str).replace(d_dict)
print (df)
id Status Country Income
0 1 Married Japan 5000-10000
1 2 UnMarried China 11000-20000
</code></pre>
<p>因此,您可以尝试将嵌套键转换为字符串:</p>
<pre><code>d_dict = {'Status' : { '4':'Married', '5':'UnMarried'},
'Country': { '2': 'Japan' , '3': 'China'},
'Income': {3: "5000-10000", 2: "11000-20000"}}
d_dict = {k: {str(k1): v1 for k1, v1 in v.items()} for k,v in d_dict.items()}
df = df.astype(str).replace(d_dict)
print (df)
id Status Country Income
0 1 Married Japan 5000-10000
1 2 UnMarried China 11000-20000
</code></pre>
<p>或将所有键转换为整数:</p>
<pre><code>d_dict = {k: {int(k1): v1 for k1, v1 in v.items()} for k,v in d_dict.items()}
df = df.replace(d_dict)
print (df)
id Status Country Income
0 1 Married Japan 5000-10000
1 2 UnMarried China 11000-20000
</code></pre>