Need help changing shader to group

Is there a way to change the diffuse shader to an existing group through python?

Here is what I’ve got so far, I’ve tried everything to make it work.
I already have the group appended from another blend file. I just want to assign it by replacing the default shader(Diffuse) to the existing group.

import bpy

# Add material and toggle use nodes.
material = bpy.data.materials.new(name="Material")
material.use_nodes = True

# Remove default diffuse shader
material.node_tree.nodes.remove(material.node_tree.nodes.get('Diffuse BSDF'))

# Adding the group doesnt seem to work, must be because active area.type is not "NodeEditor"?
#Any way of making this work?
bpy.ops.node.add_node(type="ShaderNodeGroup", settings=[{"name":"node_tree", "value":"bpy.data.node_groups['carpaint_group']"}], use_transform=True)

# Get material output
material_output = material.node_tree.nodes.get('Material Output')
# Get group 
group = material.node_tree.nodes.get('carpaint_group')


#link material output to group
material.node_tree.links.new(material_output.inputs[0], group.outputs[0])


# set active material to your new material
bpy.context.object.active_material = material


This is how I would do it:

import bpy

mat=bpy.context.active_object.active_material

mat.node_tree.nodes.remove(mat.node_tree.nodes['Diffuse BSDF'])
ntree=mat.node_tree.nodes.new('ShaderNodeGroup')
ntree.node_tree=bpy.data.node_groups['carpaint_group']
mat.node_tree.links.new(ntree.outputs[0], mat.node_tree.nodes['Material Output'].inputs['Surface'])

Awesome. Thanks!