在熊猫列表中提取列表

Posted

技术标签:

【中文标题】在熊猫列表中提取列表【英文标题】:Extracting lists within a list in pandas 【发布时间】:2015-09-10 16:52:34 【问题描述】:

我有一个返回 SUDS 对象的 API 响应,然后我使用以下命令将其转换为字典:

dict(广告系列)

我的问题是我似乎无法理解如何将此列表转换为可用的数据框。如果我试试这个:

for_merge_ids = dict(campaigns)
test = pd.DataFrame.from_dict(for_merge_ids)

字典看起来像这样:

(campaign)
     campaignID = 77705
     campaignName = "FLI - Tablet"
     campaignBid = 
        (bidInformation)
           biddingStrategy = "Cpc"
           cpcBid = 
              (CPCBid)
                 cpc = 0
              
           cpaBid = None
        
     budgetID = 0
     remainingDays = 5
     status = "RUNNING"
     categoryBids = 
        (ArrayOfCategoryBid)
           categoryBid[] = 
              (categoryBid)
                 campaignCategoryUID = 0
                 campaignID = 77705
                 categoryID = 0
                 selected = True
                 bidInformation = 
                    (bidInformation)
                       biddingStrategy = "Cpc"
                       cpcBid = 
                          (CPCBid)
                             cpc = 0
                          
                       cpaBid = None
                    
              ,
              (categoryBid)
                 campaignCategoryUID = 0
                 campaignID = 77705
                 categoryID = 0
                 selected = True
                 bidInformation = 
                    (bidInformation)
                       biddingStrategy = "Cpc"
                       cpcBid = 
                          (CPCBid)
                             cpc = 0.12
                          
                       cpaBid = None
                    
              ,
              (categoryBid)
                 campaignCategoryUID = 2289648
                 campaignID = 77705
                 categoryID = 1676592472
                 selected = True
                 bidInformation = 
                    (bidInformation)
                       biddingStrategy = "Cpc"
                       cpcBid = 
                          (CPCBid)
                             cpc = 0
                          
                       cpaBid = None
                    
              ,
              (categoryBid)
                 campaignCategoryUID = 0
                 campaignID = 77705
                 categoryID = 0
                 selected = True
                 bidInformation = 
                    (bidInformation)
                       biddingStrategy = "Cpc"
                       cpcBid = 
                          (CPCBid)
                             cpc = 0
                          
                       cpaBid = None
                    
              ,
        
   ]

(虽然我只需要广告系列(带有广告系列名称),而不需要其他广告系列(如预算、arrayofcategorybid 等)

我也尝试过指定两种方向类型。我得到一个数据框,但每个“列表”都重复并且列不被理解 - 像这样:

campaign
0  [(campaignID, 4584), (campaignName, Before Clo...
1  [(campaignID, 5304), (campaignName, Before Clo...
2  [(campaignID, 5305), (campaignName, Before Clo...
3  [(campaignID, 5598), (campaignName, After), (e...
4  [(campaignID, 5684), (campaignName, Before far...
5  [(campaignID, 5685), (campaignName, Before far...

等等..

您能帮我指出如何获取每行的标题并以 df 样式使用吗?

谢谢

【问题讨论】:

你能提供一个关于你的dict(campaigns)的样本吗? 当然,添加到问题中。 @JianxunLi 【参考方案1】:

这可能有点晚了,但试试这个,它将 sudsobject 转换为 dict:

import pandas as pd
import suds.sudsobject as sudsobject
campaigns = [sudsobject.asdict(x) for x in campaigns ]
df = pd.DataFrame(campaigns)

【讨论】:

以上是关于在熊猫列表中提取列表的主要内容,如果未能解决你的问题,请参考以下文章

如何从字典列表中提取数据到熊猫数据框中?

从列表列表中提取元素并将其分配为熊猫数据框列中的值

基于索引从大熊猫系列列表中提取到另一个大熊猫系列

从存储为熊猫数据框中的字符串的列表中提取项目

将熊猫系列时间戳转换为唯一日期列表

熊猫的嵌套列表列表