如何将此列表变成数据框架?我想知道如何在Python中做到这一点?
我在Python中有一个清单,在下面显示这样的列表。我想将其变成数据框架。我尝试过: pd.dataframe(mylist),但是'riginins'列存储 list ,但是我想存储<< strong>在同一数据框架中的原点和数量键
myList = [
{
"id":3105052,
"title":"Ebook Relat�rios Gerenciais",
"offering":"Institucional",
"created_date":"2022-06-28"
"inserted_date":"2022-06-28",
"channel":"Social",
"start_date":"2022-06-28",
"end_date":"2022-06-28",
"origins":[
{
"origin":"LinkedIn",
"quantityLeads":"1"
},
{
"origin":"Facebook",
"quantityLeads":"1"
}
]
},
{
"id":3105052,
"title":"Ebook Relat�rios Gerenciais",
"offering":"Institucional",
"inserted_date":"2022-06-28",
"created_date":"2022-06-28",
"channel":"Direct",
"start_date":"2022-06-28",
"end_date":"2022-06-28",
"origins":[
{
"origin":"Desconhecida",
"quantityLeads":"2"
}
]
},
{
"id":2918513,
"title":"Ebook Direct To Consumer",
"offering":"Supply Chain",
"created_date":"2022-06-28",
"inserted_date":"2022-06-28",
"channel":"Social",
"start_date":"2022-06-28",
"end_date":"2022-06-28",
"origins":[
{
"origin":"LinkedIn",
"quantityLeads":"1"
}
]
}
]
I have a list in python which is showing like this just below. I would like to turn it into a data frame. I tried it: pd.DataFrame(myList), however the 'origins' column stores a list, however I would like to store the origin and quantityLeads keys in that same dataframe
myList = [
{
"id":3105052,
"title":"Ebook Relat�rios Gerenciais",
"offering":"Institucional",
"created_date":"2022-06-28"
"inserted_date":"2022-06-28",
"channel":"Social",
"start_date":"2022-06-28",
"end_date":"2022-06-28",
"origins":[
{
"origin":"LinkedIn",
"quantityLeads":"1"
},
{
"origin":"Facebook",
"quantityLeads":"1"
}
]
},
{
"id":3105052,
"title":"Ebook Relat�rios Gerenciais",
"offering":"Institucional",
"inserted_date":"2022-06-28",
"created_date":"2022-06-28",
"channel":"Direct",
"start_date":"2022-06-28",
"end_date":"2022-06-28",
"origins":[
{
"origin":"Desconhecida",
"quantityLeads":"2"
}
]
},
{
"id":2918513,
"title":"Ebook Direct To Consumer",
"offering":"Supply Chain",
"created_date":"2022-06-28",
"inserted_date":"2022-06-28",
"channel":"Social",
"start_date":"2022-06-28",
"end_date":"2022-06-28",
"origins":[
{
"origin":"LinkedIn",
"quantityLeads":"1"
}
]
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在“起源”中有一个以上的元素,则可以首先爆炸,创建“ Origin”,“ QuantityLeads”,然后决定如何使用其余数据框架。
打印(DF):
If you have more than one element in "origins" you may first explode, create "origin", "quantityLeads" and then decide what to do with rest of the dataframe.
print(df):
在追求简单性时,您可以将字典结构弄平:
输出:
作为旁注,对于上方的myList示例,在第一个条目的create_date之后,造成错误后有一个丢失的逗号。
编辑:如果Origins列表中有一个可变数量的项目,但是每个项目都具有相同的键,那么我们也可以迭代这些键。
In the pursuit of simplicity you could just flatten the dictionary structures with something like:
Output:
Just as a side note, for the myList sample above there is a missing comma after the first entry's created_date that's causing an error.
EDIT: If there are a variable number of items in the origins list, but each item has the same keys then we could iterate over those as well.
它对我有用。
It's working for me.