简体   繁体   中英

List of dictionaries with multiple values per key as Dataframe

I want to convert a list of dictionaries with multiple values per key to a Dataframe.

d = [
    {'2020-12-31 00:00:00': [123, 456, 789, 321]}, 
    {'2021-05-06 00:00:00': [999, 888, 777, 666]}
]

The Dataframe should look like this:

index Date value1 value2 value3 value4
0 2020-12-31 00:00:00 123 456 789 321
1 2021-05-06 00:00:00 999 888 777 666

I know that creating a Dataframe with a list of dictionaries is easily achievable with pd.DataFrame(dictionaries) but i would like to split the values into separate columns.

I appreciate any help.

pd.DataFrame(d).stack().apply(
    pd.Series).add_prefix(
    'Value').reset_index().rename(columns={'level_0':'index', 'level_1':'Date'})

Output

    index Date               Value0 Value1  Value2  Value3
0   0     2020-12-31 00:00:00   123  456    789     321
1   1     2021-05-06 00:00:00   999  888    777     666

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM