I'm new to Python - my first experience took place 2-3 days ago - and am stuck with mapping data using df.groupby method, therefore help would be highly appriciated.
With given df:
id VALUE1 VALUE2 VALUE3
1 1 11 abc
2 1 22 def
3 2 33 ghi
4 2 44 jkl
I want to format this data to format returnable by Flask. In order to achieve that I'm starting with using a groupby method like this:
df = df.groupby('VALUE1', group_keys=False, as_index=False)
Completing that, my 'df' is not returnable by Flask, so I took another step to format this data using
df = df.groupby('VALUE1', group_keys=False, as_index=False).apply(lambda x: x.to_json(orient='records'))
This resulted in almost acceptable format:
id, value:
1 [
{ "VALUE1": "1", "VALUE2": "11", "VALUE3": "abc" },
{ "VALUE1": "1", "VALUE2": "22", "VALUE3": "def" },
]
2 [
{ "VALUE1": "2", "VALUE2": "33", "VALUE3": "ghi" },
{ "VALUE1": "2", "VALUE2": "44", "VALUE3": "jkl" },
]
Format that ultimately I would like to achieve is this:
id, value:
1 1: {
{ "VALUE2": "11", "VALUE3": "abc" },
{ "VALUE2": "22", "VALUE3": "def" },
}
2 2: {
{ "VALUE2": "33", "VALUE3": "ghi" },
{ "VALUE2": "44", "VALUE3": "jkl" },
}
Thanks!