Home > other >  python condition based dynamic json append
python condition based dynamic json append

Time:08-21

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?

CodePudding user response:

In column_status, you can specify if you want to dynamically receive the data.

column_status = {
    "group": False,
    "price1": True,
    "price2": True
}

It is possible to change this with the key of items. As group key is set to False it is not going to be shown in the dynamically rendered list.

all_items = []
for item in jsondata:
    new_item = {}
    for key, value in item.items():
        if (key in column_status) and (column_status[key]):
            new_item[key] = item[key]
    if new_item:
        all_items.append(new_item)

This is going to update the new list called all_items with the data from column_status. If column_status for the key is specified True, it will be available in the all_items.

CodePudding user response:

[ { "name": "id", "type": "STRING", "mode": "NULLABLE" }, { "name": "first_name", "type": "STRING", "mode": "NULLABLE" }, { "name": "last_name", "type": "STRING", "mode": "NULLABLE" }, { "name": "dob", "type": "DATE", "mode": "NULLABLE" }, { "name": "address", "type": "JSON", "mode": "NULLABLE" } ]

CodePudding user response:

filter by column value, then use either toJSON or groupby on top of that.

refer to this answer for more info

toJSON dataframe function

  • Related