<p>您可以使用<code>json_normalize</code>对原始JSON格式进行一些转换</p>
<ol>
<li>将JSON转换为列表格式。
我假设“阿维罗”为城市,“阿尔贝加里亚-维利亚”为地区。抱歉,我不熟悉这个地区,所以如果它是错误的,请重新命名的关键</李>
</ol>
<pre><code>res = [{**z, **{'city': x, 'district': y}} for x, y in data.items() for y, z in y.items()]
</code></pre>
<p>这将把键值样式的原始JSON转换为对象列表</p>
<pre><code>[{
"city": "Aveiro",
"district": "Albergaria-a-Velha",
"candidates": [{
...
}]
</code></pre>
<ol start=“2”>
<li>然后使用<code>json_normalize</code></李>
</ol>
<pre><code>df = pd.json_normalize(res, record_path=['candidates'], meta=['total_votes', 'city', 'district'])
</code></pre>
<ol start=“3”>
<li>进一步扩展嵌套对象<code>total_votes</code></李>
</ol>
<pre><code>df = pd.concat([df, pd.json_normalize(df['total_votes'])], axis=1)
</code></pre>
<pre><code>>>> df.iloc[0]
effectiveCandidates [JOSÉ OLIVEIRA SANTOS]
party B.E.
votes.absoluteMajority 0
votes.acronym B.E.
votes.constituenctyCounter 1
votes.mandates 0
votes.percentage 1.34
votes.presidents 0
votes.validVotesPercentage 1.4
votes.votes 179
total_votes {'availableMandates': 0, 'blankVotes': 377, 'b...
city Aveiro
district Albergaria-a-Velha
availableMandates 0
blankVotes 377
blankVotesPercentage 2.82
displayMessage None
hasNoVoting False
nullVotes 221
nullVotesPercentage 1.66
numberParishes 6
numberVoters 13351
percentageVoters 59.48
Name: 0, dtype: object
</code></pre>