'Python: Merge two dataframes skipping some rows w.r.t. to column value

Have got dataframe df1 and df2 as below:

df1

Item        Space
Grape       0.125
Mango       0.125
Mango       0.125
Beetroot    0.125
Beetroot    0.125
Beetroot    0.125
Carrot      0.125
Carrot      0.125
Carrot      0.125
Carrot      0.125

df2

Table_No    Part    Dedicated_Item      
1           1       null                    
1           2       null                
1           3       null                
1           5       Potato      
1           6       null                
1           7       null                
1           8       null                
2           1       null                
2           2       Onion
2           3       null                
2           5       null                
2           6       null                
2           7       null        
2           8       null

Need to merge df1 and df2 only to rows with 'null' value in 'Dedicated_Item' column in df2. Note: Both df1 and df2 row order should not be changed. To be added as such. If df2 got extra rows left as such.

Expected Output:

Table_No    Part    Dedicated_Item      Item        Space
1           1       null                Grape       0.125       
1           2       null                Mango       0.125   
1           3       null                Mango       0.125
1           5       Potato      
1           6       null                Beetroot    0.125
1           7       null                Beetroot    0.125
1           8       null                Beetroot    0.125
2           1       null                Carrot      0.125   
2           2       Onion
2           3       null                Carrot      0.125
2           5       null                Carrot      0.125
2           6       null                Carrot      0.125
2           7       null        
2           8       null

Thanks in Advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source