【问题标题】:Convert JSON file to Dataframe with defined format将 JSON 文件转换为具有定义格式的 Dataframe
【发布时间】:2019-05-17 02:29:10
【问题描述】:

JSON 文件由嵌入式数组组成,例如:

[[[post,post_label], [response,response_label]],[[post,post_label],[response,response_label]],…]

pd.read_json(url) 默认将文件读取到Dataframe中,如:

---------------------------------------------------
|      column01        |        column02          |
---------------------------------------------------
|   [post,post_label]  | [response,response_label]|
---------------------------------------------------
|   [post,post_label]  | [response,response_label]|
---------------------------------------------------

但是,预期的格式应该是:

---------------------------------------------------
|      column01        |        column02          |
---------------------------------------------------
|       post           |       post_label         |
---------------------------------------------------
|     response         |       response_label     | 
---------------------------------------------------
|       post           |       post_label         |
---------------------------------------------------
|     response         |       response_label     | 
---------------------------------------------------

请留言,谢谢。

【问题讨论】:

    标签: python json dataframe


    【解决方案1】:

    你可以再做一个:

    print(pd.DataFrame(df.values.flatten().tolist(),columns=df.columns))
    

    输出将根据需要。

    【讨论】:

      猜你喜欢
      • 2016-11-06
      • 1970-01-01
      • 2017-01-08
      • 2016-09-24
      • 2023-04-06
      • 2016-03-16
      • 2021-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多