You need to grab the value of the MMSA key for each item in the list of dicts, then construct a dictionary with that same value at the beginning of each key f'{row["MMSA"]} {k}': v
. This should work for you:
import json
# data retuned by your csv
csv_data = [
{"MMSA": "Manhattan, KS", "total_percent_at_risk": "47.29%", "high_risk_per_ICU_bed": "4489.84875",
"high_risk_per_hospital": "8979.6975", "icu_beds": "8", "hospitals": "4", "total_at_risk": "35918.79"},
{"MMSA": "Hilton Head Island-Bluffton-Beaufort, SC", "total_percent_at_risk": "62.72%", "high_risk_per_ICU_bed": "3904.163571",
"high_risk_per_hospital": "36438.86", "icu_beds": "28", "hospitals": "3", "total_at_risk": "109316.58"},
{"MMSA": "Kahului-Wailuku-Lahaina, HI", "total_percent_at_risk": "59.13%", "high_risk_per_ICU_bed": "3860.557",
"high_risk_per_hospital": "19302.785", "icu_beds": "20", "hospitals": "4", "total_at_risk": "77211.14"},
{"MMSA": "Spartanburg, SC", "total_percent_at_risk": "66.12%", "high_risk_per_ICU_bed": "3786.115556",
"high_risk_per_hospital": "85187.6", "icu_beds": "45", "hospitals": "2", "total_at_risk": "170375.2"},
{"MMSA": "Baton Rouge, LA", "total_percent_at_risk": "66.60%", "high_risk_per_ICU_bed": "3459.7325",
"high_risk_per_hospital": "39000.62091", "icu_beds": "124", "hospitals": "11", "total_at_risk": "429006.83"},
{"MMSA": "Rockingham County-Strafford County, NH, Metropolitan Division", "total_percent_at_risk": "57.72%",
"high_risk_per_ICU_bed": "3365.052", "high_risk_per_hospital": "40380.624", "icu_beds": "60", "hospitals": "5", "total_at_risk": "201903.12"},
{"MMSA": "Salisbury, MD-DE", "total_percent_at_risk": "68.32%", "high_risk_per_ICU_bed": "3292.271176",
"high_risk_per_hospital": "37312.40667", "icu_beds": "68", "hospitals": "6", "total_at_risk": "223874.44"},
]
# super fancy comprehension with unpacking
json_data = [{'MMSA': row['MMSA'], # keep the MMSA key
**{f'{row["MMSA"]} {k}': v # combine with the other keys and add the MMSA value
for k, v in row.items()
if k != 'MMSA'}}
for row in csv_data]
def write_json(path, data, indent=4):
with open(path, 'w') as file:
json.dump(data, file, indent=indent)
write_json('./mmsa-icu-beds.json', json_data)
json output:
[
{
"MMSA": "Manhattan, KS",
"Manhattan, KS total_percent_at_risk": "47.29%",
"Manhattan, KS high_risk_per_ICU_bed": "4489.84875",
"Manhattan, KS high_risk_per_hospital": "8979.6975",
"Manhattan, KS icu_beds": "8",
"Manhattan, KS hospitals": "4",
"Manhattan, KS total_at_risk": "35918.79"
},
{
"MMSA": "Hilton Head Island-Bluffton-Beaufort, SC",
"Hilton Head Island-Bluffton-Beaufort, SC total_percent_at_risk": "62.72%",
"Hilton Head Island-Bluffton-Beaufort, SC high_risk_per_ICU_bed": "3904.163571",
"Hilton Head Island-Bluffton-Beaufort, SC high_risk_per_hospital": "36438.86",
"Hilton Head Island-Bluffton-Beaufort, SC icu_beds": "28",
"Hilton Head Island-Bluffton-Beaufort, SC hospitals": "3",
"Hilton Head Island-Bluffton-Beaufort, SC total_at_risk": "109316.58"
},
{
"MMSA": "Kahului-Wailuku-Lahaina, HI",
"Kahului-Wailuku-Lahaina, HI total_percent_at_risk": "59.13%",
"Kahului-Wailuku-Lahaina, HI high_risk_per_ICU_bed": "3860.557",
"Kahului-Wailuku-Lahaina, HI high_risk_per_hospital": "19302.785",
"Kahului-Wailuku-Lahaina, HI icu_beds": "20",
"Kahului-Wailuku-Lahaina, HI hospitals": "4",
"Kahului-Wailuku-Lahaina, HI total_at_risk": "77211.14"
},
{
"MMSA": "Spartanburg, SC",
"Spartanburg, SC total_percent_at_risk": "66.12%",
"Spartanburg, SC high_risk_per_ICU_bed": "3786.115556",
"Spartanburg, SC high_risk_per_hospital": "85187.6",
"Spartanburg, SC icu_beds": "45",
"Spartanburg, SC hospitals": "2",
"Spartanburg, SC total_at_risk": "170375.2"
},
{
"MMSA": "Baton Rouge, LA",
"Baton Rouge, LA total_percent_at_risk": "66.60%",
"Baton Rouge, LA high_risk_per_ICU_bed": "3459.7325",
"Baton Rouge, LA high_risk_per_hospital": "39000.62091",
"Baton Rouge, LA icu_beds": "124",
"Baton Rouge, LA hospitals": "11",
"Baton Rouge, LA total_at_risk": "429006.83"
},
{
"MMSA": "Rockingham County-Strafford County, NH, Metropolitan Division",
"Rockingham County-Strafford County, NH, Metropolitan Division total_percent_at_risk": "57.72%",
"Rockingham County-Strafford County, NH, Metropolitan Division high_risk_per_ICU_bed": "3365.052",
"Rockingham County-Strafford County, NH, Metropolitan Division high_risk_per_hospital": "40380.624",
"Rockingham County-Strafford County, NH, Metropolitan Division icu_beds": "60",
"Rockingham County-Strafford County, NH, Metropolitan Division hospitals": "5",
"Rockingham County-Strafford County, NH, Metropolitan Division total_at_risk": "201903.12"
},
{
"MMSA": "Salisbury, MD-DE",
"Salisbury, MD-DE total_percent_at_risk": "68.32%",
"Salisbury, MD-DE high_risk_per_ICU_bed": "3292.271176",
"Salisbury, MD-DE high_risk_per_hospital": "37312.40667",
"Salisbury, MD-DE icu_beds": "68",
"Salisbury, MD-DE hospitals": "6",
"Salisbury, MD-DE total_at_risk": "223874.44"
}
]