How can I insert column into a nested object in python Pandas?

Viewed 119

I am working on a dataset and I have this data frame which is only a part of the dataset:

def make_decision(x):
    decisions=pd.DataFrame(([{
        "requests":" ",
        "name":fake.name(),
        # ****** NEED TO INSERT "IDS" HERE ****
        "decision":[{
            "ID":random.randint(0,400000),
             # ....
        }],
    } for i in range(x)
    ]))
    return decisions

decisions_data=make_decisions(100)

I need to use the same ids for multiple locations. For this I created another data frame:

def make_id(x):
    ids=pd.DataFrame(([{
        "id":random.randint(10000,1000000)
    } for i in range(x)]))  
    return ids

ids_data=make_id(100)

inside make_decisions, I tried:

#decisions.decision
decisions["decision"].insert(loc=0,
                     column="ID",
                     value=ids.data)

This did not work. Is there any easy way that I can insert a column inside nested data in pandas? A workaround is to create another data frame decision, insert the ids here and then insert the decision into the decisions, which is not good if I you have a deep nested dataset.

1 Answers

So, given the following toy dataframes:

print(decisions_data)
# Output
   requests name                 decision
0                  [{'fake_data': 89649}]
1                 [{'fake_data': 174821}]
2                 [{'fake_data': 174710}]
3                 [{'fake_data': 199579}]
4                 [{'fake_data': 145920}]
..      ...  ...                      ...
95                [{'fake_data': 284447}]
96                [{'fake_data': 136447}]
97                [{'fake_data': 308367}]
98                [{'fake_data': 224229}]
99                [{'fake_data': 311495}]
print(ids_data)
# Output
        id
0   392027
1   994190
2   288195
3   477688
4   123423
..     ...
95  540292
96  958907
97  856280
98  892172
99  723018

You could try this (Python 3.9+):

decisions_data["decision"] = [
    {"ID": ids_data["id"][i]} | nested_list[0]
    for i, nested_list in enumerate(decisions_data["decision"])
]

print(decisions_data)
# Output
   requests name                             decision
0                 {'ID': 559006, 'fake_data': 328753}
1                 {'ID': 381363, 'fake_data': 227269}
2                  {'ID': 627905, 'fake_data': 84975}
3                 {'ID': 951569, 'fake_data': 101472}
4                 {'ID': 329484, 'fake_data': 341664}
..      ...  ...                                  ...
95                {'ID': 511912, 'fake_data': 384229}
96                {'ID': 490489, 'fake_data': 249428}
97                 {'ID': 621211, 'fake_data': 40075}
98                {'ID': 945424, 'fake_data': 210315}
99                 {'ID': 135364, 'fake_data': 93235}
Related