Pandas:巢式merg

2024-04-20 08:58:30 发布

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

我有三个数据帧需要合并在一起。基本上,我有一个数据框架df1,它为我提供了具体的部门定义(sector)和年份。然后,我有第二个数据帧df2,它给了我一个特征foo,这里的部门变量是sector2。在

最后,我有一个匹配sector到{}的数据帧。这是一个n:1关系:一个sector2可以匹配多个{}。在

由于这些数据帧非常大,我正在寻找一种最有效的方法来合并这些数据帧,并且考虑到扇区-扇区2的链接。我不需要establishments或{}为空的行。在

我的标准合并方法已经失败了,因为n:1匹配

NotImplementedError: Index._join_level on non-unique index is not implemented

我应该如何解决这个问题?在

以下是我的数据帧示例:

  • df1型

                 establishments  employment
    year sector                             
    2005  22111               5        1853
          22112               7          70
          44511               4         264
          45211               4         637
          45299               4          80
    
  • df2型

                      foo
    Year sector2       
    2005 1111A0     19973
    2005 1111A0     17241
    2005 1111A0     13259
    2005 1111A0     13646
    2005 1111A0     13721
    
  • 我的桥

                    description  sector
    sector2                        
     1111A0     Oilseed farming   11111
     1111A0     Oilseed farming   11112
     1111B0       Grain farming   11113
     1111B0       Grain farming   11114
     1111B0       Grain farming   11115
    

Tags: 数据方法框架定义foo部门df1df2
1条回答
网友
1楼 · 发布于 2024-04-20 08:58:30

您应该在每个数据帧上生成一个reset_index,然后合并它们。 在reset_index之后,您将拥有唯一的索引,并能够执行合并。在

相关问题 更多 >