gpt4 book ai didi

Python pandas 将对象列表转换为 DataFrame

转载 作者:行者123 更新时间:2023-12-01 09:16:00 24 4
gpt4 key购买 nike

是否有一种内置方法可以将 Python 中的对象列表转换为 Pandas DataFrame。

myList = [
{
name:'ABC',
value:35,
date:2018-01-01
},
{
name:'ABC',
value:30,
date:2018-01-02
},
{
name:'DEF',
value:70,
date:2018-01-01
},
{
name:'DEF',
value:22,
date:2018-01-02
}
]

转换为

最佳答案

您可以轻松地将数据转换为数据框,然后透视以实现您想要的布局:

import pandas as pd

myList = [{"name":'ABC', "value":35, "date":"2018-01-01"},{"name":'ABC', "value":30, "date":"2018-01-02"},{"name":'DEF', "value":70, "date":"2018-01-01"},{"name":'DEF', "value":22, "date":"2018-01-02"}]
df = pd.DataFrame(data=myList)
df = df.pivot(index = "date", columns = "name")
>>> df
value
name ABC DEF
date
2018-01-01 35 70
2018-01-02 30 22

关于Python pandas 将对象列表转换为 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51248322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com