I would like to dynamically include json field based on condition.
For example if columns "group" is set to "True", then it will be visible in JSON result and used as key field in groupBy.
But if it's set to "False" then it should not appear in original dataframe, in result JSON and should not be a key field for groupBy.
Here's my dataframe. Subjected to change upon input from API whether which field will be visible in end result.
month group source amount_1 amount_2
0 2022-10-21 value_1 source1 10 100
1 2022-09-21 value_1 source1 10 100
2 2022-08-21 value_2 source2 20 50
3 2022-08-21 value_3 source1 30 50
4 2022-09-21 value_3 source1 40 60
Here's what I've tried to do.
(For amount_1
and amount_2
, they will always be visible, so they're not included in column_status
)
column_status={"group":"True","exclude":"False","source":"True"}
keys=['group','source']
all_items = []
for key,val in df.groupby(keys):
if column_status["group"]=="True": jsondata['group'] = val['group']
if column_status["source"]=="True": jsondata['source'] = val['source']
if column_status["exclude"]=="True": jsondata['exclude'] = val['exclude']
for date in all_dates:
rows = val[ val['month'] == date ].reset_index(drop=True)
price1[date]= int(rows['amount_1'].get(0, 0))
price2[date] = int(rows['amount_2'].get(0, 0))
jsondata['price1'] = price1
jsondata['price2'] = price2
all_items.append(jsondata)
Here's what I want as an end result.
[{'group': 'value_1',
'source': 'source1',
'price1': {'2022-10-21': 10, '2022-09-21': 10, '2022-08-21': 0},
'price2': {'2022-10-21': 100, '2022-09-21': 100, '2022-08-21': 0}},
{'group': 'value_2',
'source': 'source2',
'price1': {'2022-10-21': 0, '2022-09-21': 0, '2022-08-21': 20},
'price2': {'2022-10-21': 0, '2022-09-21': 0, '2022-08-21': 50}},
{'group': 'value_3',
'source': 'source1',
'price1': {'2022-10-21': 0, '2022-09-21': 40, '2022-08-21': 30},
'price2': {'2022-10-21': 0, '2022-09-21': 60, '2022-08-21': 50}}]
What would be a solution to this?