AlexW
AlexW

Reputation: 2589

python pandas - merge data conditionally

I have two panda tables with identical columns, what id like to do is merge these columns if the data in the circuit column is 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

I would like to merge these two together, but only replace any records in table 0 that have a None value for the circuit column. The final table would look like

   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

Thanks

Upvotes: 1

Views: 56

Answers (1)

Joe
Joe

Reputation: 12417

This should work:

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

Upvotes: 1

Related Questions