HOUSTON -- The Miami Marlins know what they need to do to get back into the National League postseason picture.
With timely hitting and good pitching, the Marlins are one win away from sweeping the slumping Houston Astros.
Stanton's double in the second inning broke a 2-2 tie. Christian Yelich had two hits, including an RBI single as part of the four-run frame. Valdespin hit his first homer of the season in the ninth off Chad Qualls.
The Marlins have won six of seven.
"To come here and to keep going and to keep competing and keeping that good feeling going, I'm very happy," Miami manager Mike Redmond said. "We're going to be tested. A lot of good teams left to play and a lot of ground to make up, but I'm very happy with where we're at right now."
Stanton said a sweep against the Astros would put the Marlins in a good position.
"We have to get sweeps in series like this," Stanton said. "You always say you sweep guys like this, and you stay afloat against the guys on the top. A sweep is three games gained instead of one."
The Marlins were able to capitalize on starter Jarred Cosart's wildness when he walked the bases loaded in the second, setting up the run-scoring hits by Yelich and Stanton.
"It's just who is able to capitalize the most is who is able to stay afloat and win games," Stanton said. "You aren't always going to be able to capitalize, but the more you do, the more you are going to be in good shape. We've been doing all right lately."
Tom Koehler (7-7) gave up three runs and nine hits with two strikeouts in five innings.
"I walked that tight rope that whole day and fortunately enough, I stayed balanced enough to get through five," Koehler said. "I handed it over to the bullpen who did a great job."
Jason Castro had two hits, including a two-run home run in the first that put the Astros up 2-1. Jose Altuve added three hits for Houston, which lost its fourth straight and fell a season-high 20 games below .500 (42-62).
Cosart (9-7) allowed five runs and seven hits with four strikeouts and four walks in 5 2/3 innings. The right-hander has struggled in July, allowing 21 runs in 21 1/3 innings over four starts.
"Tale of two innings, obviously," Cosart said. "Pretty self-explanatory. It's the same as always when I have a bad game. I just didn't throw strikes."
Carter cut the lead to 5-3 with an RBI single in the fifth.
Casey McGehee gave Miami a 1-0 lead with an RBI single in the first.
Houston left runners in scoring position in each inning from the second to the seventh.
Altuve and Marwin Gonzalez started the third with singles before Carter lined a ball to Koehler that went off the right-hander's back and was caught by shortstop Adeiny Hechavarria. Koehler struck out Castro and Matt Dominguez to end the inning.
"It was kind of weird," Koehler said of the out. "It used my back as a ramp to get to (Hechavarria). We looked, and after icing my shoulder, my shoulder was redder than my back. There is not really proof that I got hit, which is good."
The Astros went 1 for 9 with runners in scoring position against Koehler and finished 1 for 12 overall.
"We had some opportunities and, you know, had the right guys out there," Houston manager Bo Porter said. "We hit one line drive off the pitcher's back or shoulder and the short stop catches the ball. It's just one of those things. We had some opportunities. We were just not able to get the big hit."
After the game, Houston placed RHP Josh Zeid on the disabled list with a left foot injury. The Astros also optioned RHP Brad Peacock to Triple-A Oklahoma City to work on his fastball command, Porter said. LHP Brett Oberholtzer will start in Peacock's place Monday against Oakland. ... Houston RHP Collin McHugh will be activated from the disabled list to start on Sunday. McHugh, 4-8 with a 3.28 ERA, has been out since July 7 because of an injury to his right middle finger. McHugh, who last started in the majors on July 6 in a loss at the Los Angeles Angels, threw five innings in a rehab outing Tuesday for Oklahoma City. Miami will start RHP Jacob Turner, 3-6 with a 6.03 ERA. ... Gonzalez returned to the lineup after missing Friday's game because of soreness in his right hamstring.