Ballance, who has a career average of nearly 48, has averaged 16.75 in his last eight innings against Australia and New Zealand. He made 23 and 14 in the 405-run defeat in the second Ashes test at Lord's, which leveled the series at 1-1, and was the only change in the squad on Tuesday.Despite scoring more runs, 98, in the series so far than Adam Lyth, 50, or Ian Bell, 73, the latter two were spared.
Lyth will remain an opener with captain Alastair Cook, and Bell replaces Ballance at No. 3 in the order for the test on his home ground. Vice-captain Joe Root moves up to No. 4, and Bairstow will come in at 5.
"Gary was naturally disappointed to miss out on this occasion, however we feel that he will benefit from some time in county cricket to rediscover his form," England selector James Whitaker said in a statement.
Bairstow has played 14 tests, the last six of which have been against Australia. He featured in the 2013 home Ashes win, and played in the last two tests of the 2013-14 Ashes in Australia, scoring 10, 21, 18, 0 as England was swept aside.
He wasn't wanted by the test side after that, but he was recalled to the limited-overs scene. He led England to a 3-2 series victory this year against New Zealand, single-handedly dismantling the tourists' attack with an unbeaten 83 from 60 deliveries in the fifth ODI in Durham.
Meanwhile, he has flourished in the county championship, scoring 219 and 108 for Yorkshire in the last month.
The third test begins on July 29.