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
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]]}}