从熊猫中的json中提取嵌套值

时间:2020-07-07 12:51:59

标签: python json pandas

我有一个json文件,我已按照以下方式提取了嵌套值,但对于显示错误消息的其他嵌套值(用于天气栏):TypeError:字符串索引必须为整数。请在此处找到详细信息:enter image description here

import urllib.request, json 
import pandas as pd
from pandas.io.json import json_normalize
with urllib.request.urlopen('http://api.openweathermap.org/data/2.5/forecast?q=Copenhagen&appid=?&units=metric') as url:
    data = json.loads(url.read().decode())
    df = pd.DataFrame(json_normalize(data))
print(df)

# to extract the nested value of list column:
pd.json_normalize(data = data, record_path = "list")
# then after another column derived of weather (from list column) with nested values.
# while extract the 2nd nested values column of weather as follows;
pd.json_normalize(data = df1, record_path = "weather")

TypeError:字符串索引必须为整数

enter image description here

谢谢您的时间。

问候 MMH

0 个答案:

没有答案