python pandas-有条件地合并数据

时间:2018-07-20 11:56:03

标签: python pandas

我有两个具有相同列的熊猫表,如果circuit列中的数据为None,id想要做的就是合并这些列

>> 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 个答案:

答案 0 :(得分:1)

这应该有效:

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