无法将Json转换为 Dataframe

mfuanj7w  于 2022-11-19  发布在  其他
关注(0)|答案(2)|浏览(176)

无法将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)
  • 类型错误:JSON对象必须是字符串、字节或字节数组,而不是SimplePublicObjectWithAssociations*
    已尝试以下操作:
s1 = json.dumps(test_data)
d2 = json.loads(s1)
  • 类型错误:SimplePublicObjectWithAssociations类型的对象不是JSON可序列化的对象 *
    首选输出:

pcww981p

pcww981p1#

你能试试这个吗:

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

如果要将createdate列转换为日期时间:

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
q9yhzks0

q9yhzks02#

有一个部分的解决方案.....也许选择或做一个非枢纽 Dataframe ,这种方法可能是有用的...

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)

我已经尝试与融化和unstack,但我没有达到您的首选输出...

相关问题