【问题标题】:Python JSON to dataframe [duplicate]Python JSON到数据框[重复]
【发布时间】:2021-12-22 00:19:41
【问题描述】:

我有json格式

   {
   "projects":[
      {
         "author":{
            "id":163,
            "name":"MyApp",
            "easy_external_id":null
         },
         "sum_time_entries":0,
         "sum_estimated_hours":29,
         "currency":"EUR",
         "custom_fields":[
            {
               "id":42,
               "name":"System",
               "internal_name":null,
               "field_format":"string",
               "value":null
            },
            {
               "id":40,
               "name":"Short describe",
               "internal_name":null,
               "field_format":"string",
               "value":""
            }
         ]
      }
   ]"total_count":1772,
   "offset":0,
   "limit":1
}

而且我不知道如何将此 Json“完全”转换为数据帧。分别地,我只想要projects 中的内容。但是当我这样做时:

df = pd.DataFrame(data['projects'])

虽然我只从项目中获取数据框,但在某些列(例如:authorcustom_fields)中,格式仍将保持未分解,我也想在这些列中分解它。 谁能给点建议?

我希望:

author.id author.name author.easy_external_id sum_time_entries currency custom_fields.id custom_fields.name etc..
163 MyApp null 0 EUR 42 System ...

【问题讨论】:

  • 你能举例说明你期望 df 是怎样的吗?

标签: python json pandas dataframe


【解决方案1】:

试试:

df = pd.json_normalize(data['projects'])

请参阅文档here

【讨论】:

  • 当我这样做时: df = pd.json_normalize(data['projects']) 作者将为我分解权利(author.name、author.id 等),但 custom_fields 将保持未分解
  • 缺少答案,因为有一个嵌入字段,即 custom_fields。我已经对代码添加了更新,我相信这会起作用,因为您可以在 custon_fields 中有 N 字段。 ` df = pd.json_normalize(a['projects']) df_1 = df['custom_fields'].apply(pd.Series) counter = 0 for col in df_1.columns: res = pd.json_normalize(df_1[col]) res.columns = ['custom_fields_{}'.format(col_name+str(counter)) for col_name in res.columns] counter= counter +1 df = pd.concat([df,res],axis=1) df = df.drop(["custom_fields"],axis=1) df` @Cesc
  • @alphaBetaGamma 请显示更清晰,或通过一些链接?谢谢。
  • df = pd.json_normalize(a['projects']) #newligne df_1 = df['custom_fields'].apply(pd.Series) #newligne counter = 0 for col in df_1.columns: #newligne res = pd.json_normalize(df_1[col]) #newligne res.columns = ['custom_fields_{}'.format(col_name+str(counter)) for col_name in res.columns] #newligne counter = counter + 1 #newligne df = pd.concat([df,res],axis=1) #newligne df = df.drop(["custom_fields"],axis=1) /n df
  • 抱歉,但 cmets 不支持换行符,所以我添加了评论来解释这一点。 df = pd.json_normalize(a['projects']) #newline df_1 = df['custom_fields'].apply(pd.Series) #newline counter = 0 #newline for col in df_1.columns: #newline /// inside loop ///// res = pd.json_normalize(df_1[col]) #newline res.columns = ['custom_fields_{}'.format(col_name+str(counter)) for col_name in res.columns] #newline counter = counter + 1 #newligne df = pd.concat([df,res],axis=1) #newline ///////endloopp//// df = df.drop(["custom_fields"],axis=1) #newline df @Cesc
【解决方案2】:

我在这里尝试过,它可以工作...我认为问题出在您的 JSON 文件中。尝试做:

data = {'projects': [{'author': {'id': 163,
    'name': 'MyApp',
    'easy_external_id': None},
   'sum_time_entries': 0,
   'sum_estimated_hours': 29,
   'currency': 'EUR',
   'custom_fields': [{'id': 42,
     'name': 'System',
     'internal_name': None,
     'field_format': 'string',
     'value': None},
    {'id': 40,
     'name': 'Short describe',
     'internal_name': None,
     'field_format': 'string',
     'value': ''}]}],
 'total_count': 1772,
 'offset': 0,
 'limit': 1}

【讨论】:

  • 不在我身边。当我这样做时:df = pd.json_normalize(data['projects']) author 将为我分解正确的(author.name、author.id 等),但 custom_fields 将保持未分解。
猜你喜欢
  • 2019-09-12
  • 2020-12-02
  • 2017-09-25
  • 2017-05-14
  • 1970-01-01
  • 2021-09-02
  • 2022-11-15
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多