【问题标题】:How to convert python JSON rows to dataframe columns without looping如何在不循环的情况下将python JSON行转换为数据框列
【发布时间】:2020-10-10 17:15:36
【问题描述】:

我试图弄清楚如何在不使用循环的情况下执行以下操作。

我有一个数据框,它有几列,其中一列有一个 JSON 字符串。我想要做的是将 JSON 字符串列转换为数据框中自己的列。例如我有以下数据框:

Column 1 | column 2 | Json Column

 123     | ABC      | {"anotherNumber":345,"anotherString":"DEF"}

我想转换成这个:

Column 1 | column 2 | anotherNumber | anotherString

 123     | ABC      | 345           | DEF 

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    如有必要,您可以先将Json Column转换为dictjson.loads

    import json
    
    df = pd.DataFrame({'Column 1':[123],
                       'Column 2':['ABC'], 
                       'Json Column':['{"anotherNumber":345,"anotherString":"DEF"}']})
    print (df)
    
       Column 1 Column 2                                     Json Column
    0       123      ABC  {'anotherString': 'DEF', 'anotherNumber': 345}
    
    print (type(df.ix[0,'Json Column']))
    <class 'str'>
    
    df['Json Column'] =  df['Json Column'].apply((json.loads))
    
    print (type(df.ix[0,'Json Column']))
    <class 'dict'>
    

    然后生成列表列表并从构造函数创建Dataframe

    print (df['Json Column'].values.tolist())
    [{'anotherString': 'DEF', 'anotherNumber': 345}]
    
    df1 = pd.DataFrame(df['Json Column'].values.tolist())
    print (df1)
       anotherNumber anotherString
    0            345           DEF
    

    最后一个 concat 到原始,其中列 Json Columndrop 删除:

    print (pd.concat([df.drop('Json Column', axis=1), df1], axis=1))
       Column 1 Column 2  anotherNumber anotherString
    0       123      ABC            345           DEF
    

    【讨论】:

    • 谢谢!我以前试过这个,但没有 json.loads 片所以它不起作用。
    • 是的,在投票后我知道它是字符串列。感谢您的接受!
    【解决方案2】:
    1. 读取时String转Json的标准步骤
    import json
    import pandas as pd
    
    df = pd.DataFrame({'Column 1':[123],
                   'Column 2':['ABC'], 
                   'Json_Column':['{"anotherNumber":345,"anotherString":"DEF"}']})
    df
        Column 1    Column 2    Json_Column
    0        123         ABC    {"anotherNumber":345,"anotherString":"DEF"}
    
    df.Json_Column = df.Json_Column.apply(lambda x: json.loads(x))
    
    df.Json_Column
    0    {'anotherNumber': 345, 'anotherString': 'DEF'}
    Name: Json_Column, dtype: object
    
    1. 将每个 json 行转换为数据框
    df.Json_Column = df.Json_Column.apply(lambda x: pd.DataFrame([x]))
    
    df.Json_Column
    0       anotherNumber anotherString
    0            34...
    Name: Json_Column, dtype: object
    
    1. 将所有行中存在的数据框连接到单个数据框
    import functools
    temp_json_df = functools.reduce(lambda x,y: pd.concat([x,y]), df.Json_Column)
    
    temp_json_df
       anotherNumber    anotherString
    0            345              DEF
    
    1. 合并 master 和 temp_json_df
    df = pd.concat([df.drop(columns='Json_Column'), temp_json_df], axis=1)
    
    df
     Column 1   Column 2    anotherNumber   anotherString
    0    123         ABC              345            DEF
    

    【讨论】:

      猜你喜欢
      • 2017-03-18
      • 2020-03-15
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多