I have a data like this,
data = [{'vesselId': '1',"vesselName": "ALPHA 01",'weatherStatus': 'Good','ballastFlag':'B','milesPerMT_min': 'cycling','milesPerMT_max': 3,'milesPerMT_avg':6,"fuelPerMilesPerCargo": nan,'milesPerMT_avg': 3,'speedRange':'10-15'},
{'vesselId': '1',"vesselName": "ALPHA 01",'weatherStatus': 'Good','ballastFlag':'L','milesPerMT_min': 'cycling','milesPerMT_max': 45,"fuelPerMilesPerCargo": nan,'milesPerMT_avg': 3,'speedRange':'5-10'},
{'vesselId': '1',"vesselName": "ALPHA 01", 'weatherStatus': 'ROUGH','ballastFlag':'L','milesPerMT_min': 'reading', 'milesPerMT_max': 3.0,"fuelPerMilesPerCargo": nan,'milesPerMT_avg': 3,'speedRange':'10-15'},
{'vesselId': '1',"vesselName": "ALPHA 01", 'weatherStatus': 'ROUGH','ballastFlag':'L','milesPerMT_min': 'reading', 'milesPerMT_max': 3.0,"fuelPerMilesPerCargo": nan,'milesPerMT_avg': 3,'speedRange':'15-20'}]
I have written code to convert the data into json format:
new_data = []
not_found = True
for item in data:
for vesselId in new_data:
not_found = True
if item['vesselId'] == vesselId['vesselId']:
not_found = False
for weatherStatus in vesselId['Fuel_Performance']:
if item['weatherStatus'] == weatherStatus['weatherStatus'] :
weatherStatus['milesPerMT'].append({'milesPerMT_min':item['milesPerMT_min'], 'milesPerMT_max':item['milesPerMT_max'],'milesPerMT_avg':item['milesPerMT_avg']})
else:
vesselId['Fuel_Performance'].append({'weatherStatus':item['weatherStatus'],'ballastFlag':item['ballastFlag'], 'milesPerMT':[{'milesPerMT_min':item['milesPerMT_min'], 'milesPerMT_max':item['milesPerMT_max'],'milesPerMT_avg':item['milesPerMT_avg'],'Speed':item['speedRange']}]})
break
if not_found:
new_data.append({'vesselId':item['vesselId'],'vesselName':item['vesselName'] , 'Fuel_Performance':[{'weatherStatus':item['weatherStatus'], \
'ballastFlag':item['ballastFlag'],'milesPerMT':[{'milesPerMT_min':item['milesPerMT_min'], 'milesPerMT_max':item['milesPerMT_max'],'milesPerMT_avg':item['milesPerMT_avg']}],'fuelPerMilesPerCargo': item['fuelPerMilesPerCargo'],'Speed':item['speedRange']}]})
The output which am getting is like this ,
[{'Fuel_Performance': [{'Speed': '10-15',
'ballastFlag': 'B',
'fuelPerMilesPerCargo': nan,
'milesPerMT': [{'milesPerMT_avg': 6,
'milesPerMT_max': 3,
'milesPerMT_min': 8},
{'milesPerMT_avg': 3,
'milesPerMT_max': 45,
'milesPerMT_min': 9}],
'weatherStatus': 'Good'},
{'ballastFlag': 'L',
'milesPerMT': [{'Speed': '10-15',
'milesPerMT_avg': 3,
'milesPerMT_max': 3.0,
'milesPerMT_min': 10},
{'milesPerMT_avg': 3,
'milesPerMT_max': 3.0,
'milesPerMT_min': 10},
{'milesPerMT_avg': 3,
'milesPerMT_max': 3.0,
'milesPerMT_min': 11}],
'weatherStatus': 'ROUGH'},
{'ballastFlag': 'L',
'milesPerMT': [{'Speed': '15-20',
'milesPerMT_avg': 3,
'milesPerMT_max': 3.0,
'milesPerMT_min': 11},
{'milesPerMT_avg': 3,
'milesPerMT_max': 3.0,
'milesPerMT_min': 11}],
'weatherStatus': 'ROUGH'}],
'vesselId': '1',
'vesselName': 'ALPHA 01'}]
The way I want is like below,
[
{
"vesselId": 1,
"vesselName": "ALPHA 01",
"fuelPerformance": {
"Good": {
"B": [
{
"speed": "10 - 15",
"milesPerMT": {
"Min": 8,
"Max": 3,
"Avg": 6
},
"fuelPerMilesPerCargo": nan
}
],
"L": [
{
"speed": "5 - 10",
"milesPerMT": {
"Min": 9,
"Max": 45,
"Avg": 3
},
"fuelPerMilesPerCargo": nan
}
]
},
"Rough": {
"L": [
{
"speed": "10 - 15",
"milesPerMT": {
"Min": 10,
"Max": 3,
"Avg": 3
},
"fuelPerMilesPerCargo": nan
},
{
"speed": "15 - 20",
"milesPerMT": {
"Min": 11,
"Max": 3,
"Avg": 3
},
"fuelPerMilesPerCargo": nan
}
]
}
}
}
]
Am trying to group the values based on this below logic
If the weather_status is "good" and ballast_flag can be "B" or "L" Group all the key and values for B and L . Similarly when the weather_status is "Rough".
In my original output am not able to bring three things (speed, milesPerMT, fuelPerMilesPerCargo) inside the ballast_flag.
In my current code am not getting how to group those based on weather_status and ballast_flag