The ritual of tapping the top of a can of soda, beer, or other carbonated beverage before opening it is learned behavior, passed down from one generation to the next, though the jury is still out on just how effective it is at settling the contents of said can. According to Chris Hamlett, a chemistry lecturer at Nottingham Trent University, tapping a soda can is less about having the magic touch and more about moving bubbles around.
Hamlett wrote about the science behind the can-tapping habit for The Conversation, beginning with what causes carbonated beverages to foam in the first place. "The refreshing 'hiss' is the result of gas bubbles escaping from the liquid as a result of a change in the solubility of the carbon dioxide (CO2) in it," he explained, adding that the change in solubility occurs because of the change in pressure when the can is opened.
Tiny bubbles that line the inner walls of the unopened can increase in size when the pressure decreases (read: when the can is opened) and, according to Hamlett, "When these bubbles reach a certain size, they detach from the inside of the can and rise up to the top of the can due to buoyancy and displace liquid in their path." By tapping the can, you're effectively moving the gas bubbles and helping to clear that path, as demonstrated in the graphic below.
Images via The Conversation // CC BY-ND 4.0
But, according to How Stuff Works, there is no way of knowing whether your tapping has dislodged enough bubbles to settle a recently shaken soda, so the best plan of attack is to let the drink sit for a bit before attempting to consume it. Or you can live dangerously, skip the waiting and the tapping, and be prepared to deal with the consequences.