The evolution of empathy, and the altruism and cooperation it encourages, is a bit of a curly problem. It’s well known that groups that employ a particular minimal threshold level of altruism can potentially outcompete groups that are less cooperative. The problem is, beneath this threshold level, it’s difficult to see how empathy and altruism can gain a foothold without being drowned out by self-interest.
This is a problem that even Darwin acknowledged, and there have since been proposed a number of possible solutions, including kin selection and reciprocity. Here’s one of my own – although it’s more than likely it’s been proposed before, but I haven’t stumbled across any explicit references to it to date: