Создать словарь DataFrame
s, а затем объединить его по concat
, последний удалить второй уровень MultiIndex
, rename
it и преобразовать индекс в столбец key
:
d = {k: pd.DataFrame(v) for k, v in api_results.items()}
df = pd.concat(d).reset_index(level=1, drop=True).rename_axis('key').reset_index()
print (df)
key type suburb street oldRange \
0 VIC House Carlton North 114 Garton Street $4,400,000 - $4,800,000
1 VIC House Fitzroy North 24 Egremont Street $1,600,000 - $1,750,000
2 VIC House Hamlyn Heights 6 Heritage Drive $1,180,000 - $1,280,000
3 VIC House Caulfield South 24 Emma Street $1,900,000 - $2,089,999
newRange dollarDelta percentDelta daysDelta
0 $4,100,000 -$700,000 -14% 42
1 $1,950,000 - $2,100,000 $350,000 20% 62
2 $990,000 - $1,050,000 -$230,000 -17% 82
3 $1,800,000 - $1,980,000 -$109,999 -5% 33
Если возможно, создайте DataFrame
s в первом l oop:
api_results = {}
for items in States:
url = url_swing + items
request = requests.get(url)
api_results[items]= pd.DataFrame(request.json())
df = pd.concat(api_results).reset_index(level=1, drop=True).rename_axis('key').reset_index()