如何使用pandas从api展平json

如何使用pandas从api展平json,json,python-3.x,pandas,list,json-normalize,Json,Python 3.x,Pandas,List,Json Normalize,我有一个json从我要添加到列表的API返回。在我打完电话后,我需要使用pandas将数据展平。我不知道该怎么办 代码: 当我调用api\u resultsI时,我的数据如下所示: [{"requesturl":"http:\/\/www.odg-twc.com\/index.html?calculator.htm?icd=840.9~719.41&age=-1&state=IL&jobclass=1&cf=4","

我有一个
json
从我要添加到列表的API返回。在我打完电话后,我需要使用pandas将数据展平。我不知道该怎么办

代码:

当我调用
api\u results
I时,我的数据如下所示:

[{"requesturl":"http:\/\/www.odg-twc.com\/index.html?calculator.htm?icd=840.9~719.41&age=-1&state=IL&jobclass=1&cf=4","clientid":"123456789","adjustedsummaryguidelines":{"midrangeallabsence":46,"midrangeclaims":36,"atriskallabsence":374,"atriskclaims":98},"riskassessment":{"score":87.95,"status":"Red (Extreme)","magnitude":"86.65","volatility":"89.25"},"adjustedduration":{"bp":{"days":2},"cp95":{"alert":"yellow","days":185},"cp100":{"alert":"yellow","days":365}},"icdcodes":[{"code":"719.41","name":"Pain in joint, shoulder region","meandurationdays":{"bp":18,"cp95":72,"cp100":93}},{"code":"840.9","name":"Sprains and strains of unspecified site of shoulder and upper arm","meandurationdays":{"bp":10,"cp95":27,"cp100":35}}],"cfactors":{"legalrep":{"applied":"1","alert":"red"}},"alertdesc":{"red":"Recommend early intervention and priority medical case management.","yellow":"Consider early intervention and priority medical case management."}}
,{"clientid":"987654321","adjustedsummaryguidelines":{"midrangeallabsence":25,"midrangeclaims":42,"atriskallabsence":0,"atriskclaims":194},"riskassessment":{"score":76.85,"status":"Orange (High)","magnitude":"74.44","volatility":"79.25"},"adjustedduration":{"bp":{"days":2},"cp95":{"days":95},"cp100":{"alert":"yellow","days":193}},"icdcodes":[{"code":"724.2","name":"Lumbago","meandurationdays":{"bp":10,"cp95":38,"cp100":50}},{"code":"847.2","name":"Sprain of lumbar","meandurationdays":{"bp":10,"cp95":22,"cp100":29}}],"cfactors":{"legalrep":{"applied":"1","alert":"red"}},"alertdesc":{"red":"Recommend early intervention and priority medical case management.","yellow":"Consider early intervention and priority medical case management."}}]
我一直在使用
json\u normalize
,但我知道我没有正确使用这个库

如何将这些数据展平

我需要的是:

+---------+----+------+----+------+----+----------------+------------+------------------+--------------+--------------------+-----+-------+---------+-----+-------------+----------+------+---+-----+----+
| clientid|days| alert|days| alert|days|atriskallabsence|atriskclaims|midrangeallabsence|midrangeclaims|           alertdesc|alert|applied|magnitude|score|       status|volatility|  code| bp|cp100|cp95|
+---------+----+------+----+------+----+----------------+------------+------------------+--------------+--------------------+-----+-------+---------+-----+-------------+----------+------+---+-----+----+
|123456789|   2|yellow| 365|yellow| 185|             374|          98|                46|            36|[Recommend early ...|  red|      1|    86.65|87.95|Red (Extreme)|     89.25|719.41| 18|   93|  72|
|123456789|   2|yellow| 365|yellow| 185|             374|          98|                46|            36|[Recommend early ...|  red|      1|    86.65|87.95|Red (Extreme)|     89.25| 840.9| 10|   35|  27|
|987654321|   2|yellow| 193|  null|  95|               0|         194|                25|            42|[Recommend early ...|  red|      1|    74.44|76.85|Orange (High)|     79.25| 724.2| 10|   50|  38|
|987654321|   2|yellow| 193|  null|  95|               0|         194|                25|            42|[Recommend early ...|  red|      1|    74.44|76.85|Orange (High)|     79.25| 847.2| 10|   29|  22|
+---------+----+------+----+------+----+----------------+------------+------------------+--------------+--------------------+-----+-------+---------+-----+-------------+----------+------+---+-----+----+
  • 由于所需的结果是来自
    'icdcodes'
    键中每个
    dict
    的数据有一个单独的行,因此最好的选择是使用
    pandas.json\u规范化
  • 首先创建主数据框,并使用
    pandas.dataframe.explode('icdcodes')
    ,它将根据
    'icdcodes'
    中的dicts
    的数字,展开数据框,使每个
    'clientid'
    具有适当的行数
  • 'icdcodes'
    列上使用
    .json\u normalize()
    ,这是
    目录的
    列表,其中一些
    值也可能是
    目录
  • 。连接两个数据帧并删除
    'icdcodes'
  • 根据需要,使用
    pandas.DataFrame.rename()
    重命名列,使用
    pandas.DataFrame.drop()
    删除不需要的列
  • 从中也可以看出这一点
将熊猫作为pd导入
#根据api_结果创建初始数据帧
df=pd.json\u规范化(api\u结果)。分解('icdcodes')。重置索引(drop=True)
#仅为icdcodes创建一个数据帧,它将展开所有DICT列表
icdcodes=pd.json\u规范化(df.icdcodes)
#将df连接到icdcodes并删除icdcodes列
df=df.join(icdcodes).drop(['icdcodes'],axis=1)
#显示(df)
请求URL客户ID调整的汇总指南。中范围索赔调整的汇总指南。中范围索赔调整的汇总指南。atriskallabsence调整的汇总指南。atriskallabsence风险评估。评分风险评估。状态风险评估。重大风险评估。波动性调整的持续时间。bp.days调整的持续时间。cp95.alertadjustedduration.cp95.days adjustedduration.cp100.alert adjustedduration.cp100.days cfactors.legalrep.applied cfactors.legalrep.alert alertdesc.red alertdesc.yellow代码名称Mean DurationDays.bp Mean DurationDays.cp95 Mean DurationDays.cp100
0 http:\/\/www.odg-twc.com\/index.html?calculator.htm?icd=840.9~719.41&age=-1&state=IL&jobclass=1&cf=41234567846 36 374 98 87.95红色(极端)86.65 89.25 2黄色185黄色365 1红色建议早期干预和优先医疗病例管理。考虑早期干预和优先医疗病例管理。719.41肩关节疼痛18 72 93
1 http:\/\/www.odg-twc.com\/index.html?calculator.htm?icd=840.9~719.41&age=-1&state=IL&jobclass=1&cf=4 123456789 46 36 374 98 87.95红色(极端)86.65 89.25 2黄色185黄色365 1红色建议早期干预和优先医疗病例管理。考虑早期干预和优先医疗病例管理。840.9肩部和上臂未指定部位的扭伤和拉伤10 27 35
2 NaN 987654321 25 42 0 194 76.85橙色(高)74.44 79.25 2南95黄色193 1红色建议早期干预和优先医疗病例管理。考虑早期干预和优先医疗病例管理。724.2腰痛10 38 50
3 NaN 987654321 25 42 0 194 76.85橙色(高)74.44 79.25 2南95黄色193 1红色建议早期干预和优先医疗病例管理。考虑早期干预和优先医疗病例管理。847.2
+---------+----+------+----+------+----+----------------+------------+------------------+--------------+--------------------+-----+-------+---------+-----+-------------+----------+------+---+-----+----+
| clientid|days| alert|days| alert|days|atriskallabsence|atriskclaims|midrangeallabsence|midrangeclaims|           alertdesc|alert|applied|magnitude|score|       status|volatility|  code| bp|cp100|cp95|
+---------+----+------+----+------+----+----------------+------------+------------------+--------------+--------------------+-----+-------+---------+-----+-------------+----------+------+---+-----+----+
|123456789|   2|yellow| 365|yellow| 185|             374|          98|                46|            36|[Recommend early ...|  red|      1|    86.65|87.95|Red (Extreme)|     89.25|719.41| 18|   93|  72|
|123456789|   2|yellow| 365|yellow| 185|             374|          98|                46|            36|[Recommend early ...|  red|      1|    86.65|87.95|Red (Extreme)|     89.25| 840.9| 10|   35|  27|
|987654321|   2|yellow| 193|  null|  95|               0|         194|                25|            42|[Recommend early ...|  red|      1|    74.44|76.85|Orange (High)|     79.25| 724.2| 10|   50|  38|
|987654321|   2|yellow| 193|  null|  95|               0|         194|                25|            42|[Recommend early ...|  red|      1|    74.44|76.85|Orange (High)|     79.25| 847.2| 10|   29|  22|
+---------+----+------+----+------+----+----------------+------------+------------------+--------------+--------------------+-----+-------+---------+-----+-------------+----------+------+---+-----+----+