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 1254
没有蜡笔的小新
没有蜡笔的小新 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:12

    Try this:

    import requests
    import pandas as pd
    def call_api(json_data):
        r = requests.post(url, headers=headers, data=out)
        print(r.json())
    
    df.apply(lambda x:call_api(x.to_json()),axis=1)
    

    Hope this helps!

    0 讨论(0)
  • 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]]}}
    
    0 讨论(0)
  • 2021-01-27 17:18

    you can try this hope it will work:

    import pandas as pd
    df = pd.read_csv(your file path)
    df.to_json()
    
    0 讨论(0)
提交回复
热议问题