I have a dataframe that I would like to convert to json format by selecting the columns. And since I have a lot of lines, I can't do everything by hand
I have a dataframe that looks this :
Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4', np.nan, 'Ford', 'Audi A1'],
'Price': [22000,25000,27000,35000, 29000, 27000, 35000],
'Liscence Plate': ['ABC 123', 'XYZ 789', 'CBA 321', 'ZYX 987', 'DEF 456', 'DEF 466', 'ABC 123']}
df = pd.DataFrame(Cars,columns= ['Brand', 'Price', 'Liscence Plate'])
Brand Price Liscence Plate
0 Honda Civic 22000 ABC 123
1 Toyota Corolla 25000 XYZ 789
2 Ford Focus 27000 CBA 321
3 Audi A4 35000 ZYX 987
4 NaN 29000 DEF 456
5 Ford 27000 DEF 466
6 Audi A1 35000 ABC 123
And I have to convert to this :
data = {"form": [
{"Liscence Plate": "ABC 123",
"Brand": ["Honda Civic", "Audi A1"
],
"Price": ["22000", "35000"]},
{"Liscence Plate": "XYZ 789",
"Brand": ["Toyota Corolla",
],
"Price": ["25000"]},
{"Liscence Plate": "CBA 321",
"Brand": ["Ford Focus",
],
"Price": ["27000"]},
{"Liscence Plate": "ZYX 987",
"Brand": ["Audi A4",
],
"Price": ["35000"]},
{"Liscence Plate": "DEF 456",
"Brand": ["NaN", "Ford"
],
"Price": ["29000", "27000"]}