【问题标题】:How to extract values in a JSON file into separate columns in a dataframe row [closed]如何将 JSON 文件中的值提取到数据框行中的单独列中[关闭]
【发布时间】:2021-04-29 18:52:39
【问题描述】:
data = json.load(open("C:/Users/<username>/Downloads/one-day-run-record.json","rb"))

df = pd.json_normalize(data)[["summaries", "tags.com.nike.weather", "tags.com.nike.name", "start_epoch_ms", "end_epoch_ms", "metrics"]]
df

我的主要目标是提取metrics 列中的值。要了解该列的结构,您可以使用下面的行

df.metrics[0]

在下面的代码中,您可以看到按类型分隔的指标。我想要stepsspeedpace 类型存储在values 中的所有值

prov = pd.json_normalize(df.metrics[0])
prov

例如:输入steps 你有这个(你可以检查df.metrics[0]):

{'type': 'steps',
  'unit': 'STEP',
  'source': 'com.nike.running.android.fullpower',
  'appId': 'com.nike.sport.running.droid',
  'values': [{'start_epoch_ms': 1605042906780,
    'end_epoch_ms': 1605042907751,
    'value': 13},

   {'start_epoch_ms': 1605042907780,
    'end_epoch_ms': 1605042911754,
    'value': 11},

   {'start_epoch_ms': 1605042911772,
    'end_epoch_ms': 1605042915741,
    'value': 6},

   {'start_epoch_ms': 1605042915741,
    'end_epoch_ms': 1605042918713,
    'value': 13},

   {'start_epoch_ms': 1605042918713,
    'end_epoch_ms': 1605042920746,
    'value': 5},
    
...}]}

我想要一行包含值 [13, 11, 6, 13, 5, ...],这些值中的每一个都位于不同的数据框列中。

做起来难吗?我怎么能那样做?我尝试了多种方法,但我对 .json 文件完全陌生

【问题讨论】:

    标签: python json pandas json-normalize


    【解决方案1】:
    • 'metrics' 中的'values' 列是listdicts
      • 为了提取'value'lists 需要用.explode() 扩展,以便每个dict 位于单独的行中。
      • 'values' 现在是dicts 的一列,需要转换成dataframe。
    import pandas as pd
    import json
    from pathlib import Path
    
    # path to JSON file
    p = Path('test.json')
    
    # load the JSON file into a python object
    with p.open('r', encoding='utf-8') as f:
        data = json.loads(f.read())
    
    # convert the metrics key into a dataframe
    df = pd.json_normalize(data, 'metrics', ['id', 'start_epoch_ms', 'end_epoch_ms'])
    
    # explode the values column
    dfe = df.explode('values').reset_index(drop=True)
    
    # convert the column of dicts into a dataframe and join it back to dfe
    dfj = dfe.join(pd.DataFrame(dfe.pop('values').values.tolist()), rsuffix='_values')
    
    # groupby the type column and then aggregate the value column into a list
    dfg = dfj.groupby('type')['value'].agg(list).reset_index(name='values_list')
    
    # merge the desired list of values back to df
    df = df.merge(dfg, on='type').drop(columns=['values'])
    
    # select the final types
    desired = df.loc[df['type'].isin(['steps', 'speed', 'pace'])]
    
    # to separate each value in the list to a separate column
    final = pd.DataFrame(desired.values_list.to_list(), index=desired.type.to_list())
    
    # display(final.iloc[:, :5])
                   0          1         2          3         4        ...
    steps  13.000000  11.000000  6.000000  13.000000  5.000000        ...
    speed   0.000000   0.000000  0.000000   0.000000  0.000000        ...
    pace    8.651985   8.651985  6.542049   6.542049  6.173452        ...
    
    # aggregate calculations
    final.agg({'steps': 'sum', 'speed': 'mean', 'pace': 'mean'}, axis=1)
    
    steps    2676.000000
    speed       9.657251
    pace        5.544723
    dtype: float64
    

    数据帧截图

    • 数据框中的数据太多,无法发布文本示例,因此这里有一些屏幕截图可以帮助您了解细分

    初始df

    • 9 总行

    dfe

    • 分解列总共会创建 699 行

    dfj

    • 从该列创建一个数据框并将其加入dfe

    dfg

    • 创建所需值的列表

    最终df

    • values_list 是所需的值

    desired

    • 只选择了所需的'types'

    【讨论】:

    • 我只是有一个小问题。当我运行 df = pd.json_normalize(data, 'metrics', ['id', 'start_epoch_ms', 'end_epoch_ms']) 时,我收到此错误:TypeError: {'id': 'test', 'type': 'run',[...]'app_id': 'com.nike.sport.running.droid', 'source': 'com.nike.running.android.fullpower'}]} has non iterable value 1605042901601 for path ['start_epoch_ms']. Must be iterable or null.。你知道为什么它对你有用,但对我没有用吗?
    • @ApoloReis 我可以使用来自pastebin.com/4eNscEsh 的原始样本运行它。看看您是否可以运行该示例。另外,请确保 pandas 已更新到 1.2.1 版
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2014-10-30
    • 2018-11-09
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多