代码之家  ›  专栏  ›  技术社区  ›  AlexW

python pandas-有条件地合并数据

  •  1
  • AlexW  · 技术社区  · 6 年前

    我有两个具有相同列的panda表,我想做的是,如果circuit列中的数据为none,则合并这些列

    >> site_routing_data[0]
       circuit errors        route            site           mask      next_hop error
    0     MPLS   None   10.10.1.0        Manchester  255.255.255.0    172.5.1.5   NaN
    1     MPLS   None   10.10.2.0        London      255.255.255.0    172.5.1.5   NaN
    2     None   None   10.10.3.0        Birmingham  255.255.255.0    172.5.1.5   NaN
    3     MPLS   None   10.10.4.0        Sheffield   255.255.255.0    172.5.1.5   NaN
    
    >> site_routing_data[1]
       circuit errors        route            site           mask      next_hop error
    0     FTTC   None   10.10.1.0        Manchester  255.255.255.0    172.10.1.6   NaN
    1     FTTC   None   10.10.2.0        London      255.255.255.0    172.10.1.7   NaN
    2     FTTC   None   10.10.3.0        Birmingham  255.255.255.0    172.10.1.8   NaN
    3     FTTC   None   10.10.4.0        Sheffield   255.255.255.0    172.10.1.9   NaN
    

    我想将这两个数据合并在一起,但只替换表0中对于circuit列没有值的任何记录。最后一张桌子看起来像

       circuit errors        route            site           mask      next_hop error
    0     MPLS   None   10.10.1.0        Manchester  255.255.255.0    172.5.1.5   NaN
    1     MPLS   None   10.10.2.0        London      255.255.255.0    172.5.1.5   NaN
    2     FTTC   None   10.10.3.0        Birmingham  255.255.255.0    172.10.1.8  NaN
    3     MPLS   None   10.10.4.0        Sheffield   255.255.255.0    172.5.1.5   NaN
    

    谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   Joe    6 年前

    这应该有效:

    df.loc[df.circuit.isnull(), df.columns] = df1[df1.columns]