How to convert the row-wise data of dataframe with its column name as key and row data as value in json using python

后端 未结 3 1252
没有蜡笔的小新
没有蜡笔的小新 2021-01-27 16:16

I am having the issue in converting the row-wise data of dataframe with the column name as key and row data as value. I want to pass this row-wise json to another API a

3条回答
  •  盖世英雄少女心
    2021-01-27 17:15

    It seems that your target data format is newline-delimited JSON. In pandas you can convert your dataframe to newline-delimited JSON file using to_json() method of dataframe with setting lines parameter to True:

    Data preparation part:

    import pandas as pd
    import json
    
    data_json = [
        {
            'C_ID' : '1',
            'Latlong' : {
                '__type' : 'GeoPoint',
                'latitude' : [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
            }
        },
        {
            'C_ID' : '2',
            'Latlong' : {
                '__type' : 'GeoPoint',
                'latitude' : [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
            }
        },
        {
            'C_ID' : '3',
            'Latlong' : {
                '__type' : 'GeoPoint',
                'latitude' : [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
            }
        }]
    
    data = pd.read_json(json.dumps(data_json))
    print(data) 
    

    Output:

       C_ID                                            Latlong
    0     1  {'__type': 'GeoPoint', 'latitude': [[1, 2], [3...
    1     2  {'__type': 'GeoPoint', 'latitude': [[1, 2], [3...
    2     3  {'__type': 'GeoPoint', 'latitude': [[1, 2], [3...
    

    Writing dataframe to json file in newline-delimited format:

    data.to_json(path_or_buf='/path/to/target/json/file.json', # path to json file to write data
                 orient='records',
                 lines=True)
    

    Output file data:

    {"C_ID":1,"Latlong":{"__type":"GeoPoint","latitude":[[1,2],[3,4],[5,6],[7,8],[9,10]]}}
    {"C_ID":2,"Latlong":{"__type":"GeoPoint","latitude":[[1,2],[3,4],[5,6],[7,8],[9,10]]}}
    {"C_ID":3,"Latlong":{"__type":"GeoPoint","latitude":[[1,2],[3,4],[5,6],[7,8],[9,10]]}}
    

提交回复
热议问题