'How to Flatten a Dictionary in a Dataframe [duplicate]

I would like to flatten a dictionary that is inside the dataframe. Below is my data:

import pandas as pd
data = {'ID': [0,1],
        'Name': ['Lucas', 'Benjamin'],
        'Records': [{'Date':'2022-05-19', 'TimeIn': '7:00', 'TimeOut': '15:00'},
                    {'Date':'2022-05-19', 'TimeIn': '8:00', 'TimeOut': '14:00'}]}
sample = pd.DataFrame(data)

I want my output to be:

ID  Name      Date          TimeIn    TimeOut
0   Lucas     2022-05-19    7:00      15:00
1   Benjamin  2022-05-19    8:00      14:00


Solution 1:[1]

Do this,

pd.concat([df.drop(columns="Records"), pd.DataFrame(df["Records"].tolist())], axis=1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1