【问题标题】:Unable to covert Json to Dataframe无法将 Json 转换为 Dataframe
【发布时间】:2022-11-16 08:26:18
【问题描述】:

无法将 Json 转换为 Dataframe,出现以下 TypeError:

创建了以下数据

Test_data =

{'archived': False,
 'archived_at': None,
 'associations': None,
 'created_at': datetime.datetime(2020, 10, 30, 8, 3, 54, 190000, tzinfo=tzlocal()),
 'id': '12345',
 'properties': {'createdate': '[![2020-10-30T08:03:54.190Z][1]][1]',
                'email': 'testmail@gmail.com',
                'firstname': 'TestFirst',
                'lastname': 'TestLast'},
 'properties_with_history': None,
 'updated_at': datetime.datetime(2022, 11, 10, 6, 44, 14, 5000, tzinfo=tzlocal())}

data = json.loads(test_data)

TypeError:JSON 对象必须是 str、bytes 或 bytearray,而不是 SimplePublicObjectWithAssociations

已尝试以下方法:

s1 = json.dumps(test_data)
d2 = json.loads(s1)

TypeError:SimplePublicObjectWithAssociations 类型的对象不是 JSON 可序列化的

首选输出:

【问题讨论】:

    标签: python json pandas dataframe api


    【解决方案1】:

    你能试试这个吗:

    df=pd.json_normalize(Test_data)
    print(df)
    '''
        archived    archived_at associations    created_at                  id   properties_with_history    updated_at  properties.createdate   properties.email    properties.firstname
    0   False       None        None            2020-10-30T08:03:54.190Z    12345       2022-11-10T06:44:14.500Z    [![2020-10-30T08:03:54.190Z][1]][1] testmail@gmail.com  TestFirst
    
    '''
    

    如果你想要特定的列:

    df = df[['id','properties.createdate','properties.email','properties.firstname','properties.lastname']]
    df.columns = df.columns.str.replace('properties.', '')
    df
    
        id      createdate                              email              firstname    lastname
    0   12345   [![2020-10-30T08:03:54.190Z][1]][1] testmail@gmail.com  TestFirst   TestLast
    

    如果你想转换创建日期日期时间列:

    import datefinder
    df['createdate']=df['createdate'].apply(lambda x: list(datefinder.find_dates(x))[0])
    df
        id      createdate                          email               firstname   lastname
    0   12345   2020-10-30 08:03:54.190000+00:00    testmail@gmail.com  TestFirst   TestLast
    
    

    【讨论】:

    • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。 - From Review
    【解决方案2】:

    有一个部分解决方案......也许选择或做一个逆轴数据框这种方法可能有用......

    import pandas as pd 
    import datetime
    import json
    import jsonpickle
    
    test_data ={'archived': False,
    'archived_at': None,
    'associations': None,
    'created_at': datetime.datetime(2020, 10, 30, 8, 3, 54, 190000),
    'id': '12345',
    'properties': {'createdate': '[![2020-10-30T08:03:54.190Z][1]][1]',
                'email': 'testmail@gmail.com',
                'firstname': 'TestFirst',
                'lastname': 'TestLast'},
    'properties_with_history': None,
    'updated_at': datetime.datetime(2022, 11, 10, 6, 44, 14, 5000)}
    
    
    data = jsonpickle.encode(test_data, unpicklable=False)
    pd.read_json(data)
    

    我试过 melt 和 unstack 但我没有达到你喜欢的输出......

    【讨论】:

      猜你喜欢
      • 2015-01-05
      • 2017-04-13
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      相关资源
      最近更新 更多