结合两个数据帧的最佳方法

2024-09-27 21:30:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据帧(目前)要合并:

                head_site_df = pd.DataFrame(head_sitetupaggr_list, columns =['Die Loc', 'X Coord', 'Y Coord'])
                regvalfile_df = pd.DataFrame(regvaltupaggr_list)

regvalfile_df包含800多个动态生成的列。因此,我无法列出列。 我现在之所以这么说是因为情况有点复杂。我需要生成一个excel表格,如下所示:

enter image description here

此数据来自日志文件。在K列之后,可以有800个或更多列具有整数值的标题。这些列包含在regvalfile_df中。其他DF捕获列“Die Loc”和“X Coord”、“Y Coord”。我打算修改我的代码,最终使第二个DF也能够捕获列A到K。因此,我总共有两个DF。一个是从A到K的,另一个是从L开始的

我的问题是,为了结合这两个单独的DFs来获得excel表,最好的方法是什么。Cocatenate()、Merge()、Join()或Append()?速度和内存消耗的最有效方法是什么。我会合并两只熊猫。除非让一个DF捕捉所有东西更有效。我不知道它是如何工作的,因为L Forwards的每个文件都有“动态”金额头

到目前为止的代码示例。请注意,代码是有效的。我刚刚取出了一大块数据,其中包含到目前为止我使用过的数据结构:

    for odfslogp_obj in odfslogs_plist:
    with zipfile.ZipFile(odfslogp_obj, mode='r') as z:
        for name in z.namelist():
            dfregval = pd.DataFrame()
            with z.open(name) as etest_zip:
                for head_site, loclist in zip(head_siteparam_tup_list, linesineed):  #is there a way to turn this all into a function inside a list comprehension?
                    regvals_ext = [x for x in loclist if pattern.search(x)]
                    #print(regvals_ext)
                    regvaltups_list = [tuple(x.split(":")[0:2]) for x in regvals_ext]
                    
                    regvaldict = dict(regvaltups_list)
                    regvaltupaggr_list.append(regvaldict)
                    head_siteloc_tup = (head_site[1], head_site[0].split(',')[0], head_site[0].split(',')[1])
                    # print(head_siteloc_tup)
                    head_sitetupaggr_list.append(head_siteloc_tup)

                #print(head_sitetupaggr_list)
                head_site_df = pd.DataFrame(head_sitetupaggr_list, columns =['Die Loc', 'X Coord', 'Y Coord'])
                regvalfile_df = pd.DataFrame(regvaltupaggr_list)

       

Tags: 数据indataframedfforsiteheadlist

热门问题