everyone!
I am trying to convert a Pandas Dataframe (data1
) into specific json format string (data2
).
How do I do that?
data1 = pd.DataFrame(
{
"country": ["USA", "USA", "USA", "Canada", "Canada", "Canada", "Japan", "Japan", "Germany", "Germany", "Germany", "Germany"],
"city": ["Boston", "NY", "LA", "Montreal", "Vancouver", "Calgary", "Osaka", "Kyoto","Berlin", "Frankfurt", "Hamburg", "Dresden"]
}
)
……..
data2 = [
{"country": "USA", "city": ["Boston", "NY", "LA"]},
{"country": "Canada", "city": ["Montreal", "Vancouver", "Calgary'"]},
{"country": "Japan", "city": ["Kyoto", "Osaka"]},
{"country": "Germany", "city": ["Berlin", "Frankfurt", "Hamburg", "Dresden"]}
]
Originally data1
is coming from SQL. After converting it to to data2
I will pass it further to JS (via data2_json = json.dumps(data, indent=4)
)and other Python file to be displayed in a WebApp.
Thanks for any help in advance.
3
Answers
Use
groupby.agg
andto_dict
:Output:
Or
to_json
for a json string:Output:
This should do it for you. They key here is
groupby
using countryYou can group the pandas dataframe by the "country" column and then convert the grouped data into JSON:
You can remove the
indent
argument if you prefer your JSON unformatted.