MEXICO CITY—As Mexico’s first female president, Claudia Sheinbaum is taking over a country that her popular mentor, departing President Andrés Manuel López Obrador , has profoundly changed in just a few months since her landslide victory.

In his final major act last month, López Obrador rammed a judicial-system overhaul through congress, forcing federal judges to face elections. The legislation—which Sheinbaum says she supported—divided the country and swiftly reshaped the economic and geopolitical issues she will contend with as president.

U.S. officials say the judicial changes risk derailing the two countries’ trade ties. Mexican Supreme Court judges say the legislation undermines the checks and balances underpinning Mexico’s democracy. And financial markets and investors say Mexico’s economic outlook has dimmed, reflected in stalled foreign investment and a peso that has fallen more than 15% against the dollar since the June election.

Sheinbaum will take office with empty coffers, increased territorial control and extortion by criminal gangs, and growing uncertainty over private investment because of the judiciary shake-up, said Carlos Heredia, a longtime pro-democracy activist.

“It is a toxic inheritance,” said Heredia, who currently teaches at Mexico’s CIDE university. “The fear is that the rule of law no longer matters and that the concentration of power will continue.”

A politician who ran on cementing policies set forth by the charismatic López Obrador, Sheinbaum must now confront the reality that her mentor’s legacy will complicate her presidency, political analysts said. It threatens to distract from challenges such as fighting organized crime, renewing a free-trade pact with the U.S. and Canada and managing a potentially thorny relationship with the White House, should former President Donald Trump win November’s election.

Sheinbaum, a 62-year-old scientist who served as mayor of Mexico City, has pledged to continue López Obrador’s agenda: large cash handouts for Mexico’s poor and greater state control over key economic sectors.

She has the skills to manage the complex relationship between two countries that share a 2,000-mile border and are each other’s top trading partner, say people familiar with her work. But her task could be upended if Trump wins the election.

“Mexico is likely to be in the firing line” if Trump returns to the White House, London-based consulting firm Capital Economics said in a note to investors on Friday. Trump would target the U.S. trade relationship with Mexico, imposing high import tariffs. He has vowed to deport millions of migrants and threatened military strikes against drug gangs in Mexico.

A win by Kamala Harris would continue the close coordination of efforts that have helped the Biden administration curb U.S.-bound migration in recent months , these people say. A U.S. delegation led by Jill Biden flew to Mexico City to attend Tuesday’s inauguration.

However, in a nod to U.S. unions affected by the relocation of manufacturing plants to Mexico, Harris said on Thursday she was one of only 10 senators to vote against the U.S.-Mexico-Canada Agreement signed under Trump, which comes up for review in 2026.

“Many who voted for this deal conditioned their support on a review process, which as President I will use,” she wrote on X. “Enough is enough.”

But the U.S. needs Mexico to contain China in the world trade arena, said Alejandro Werner , Mexico’s former deputy finance minister. After tension and complex discussions, both countries are likely to cut a deal, he thinks.

“The economic and geopolitical logic of reaching agreement is very clear,” said Werner, who also served as chief of the Western Hemisphere unit of the International Monetary Fund.

Mexico’s disruptive judicial overhaul, done as the last move of a departing president whose policies often hit a roadblock in the country’s courts, can hamper trade negotiations and damage the strong ties built since the creation of the North American Free Trade Agreement three decades ago, U.S. investors and diplomats say.

The replacement of all federal judges and Supreme Court justices by others elected by popular vote next year and in 2027 risks “creating a system where we cannot rely on Mexico,” Rep. Greg Stanton, a Democratic congressman for Arizona, and ten U.S. legislators from both parties wrote in a recent letter to Secretary of State Antony Blinken and U.S. Trade Representative Katherine Tai .

Ken Salazar, the U.S. ambassador to Mexico, has warned that the judicial overhaul would damage the country’s democracy and imperil trade with the U.S. Foreign companies fear they won’t get a fair hearing from judges in the event of disputes with the government. Sheinbaum has met with many investors and U.S. executives to reassure them that the overhaul won’t be bad for business and will strengthen the fight against organized crime.

She also told members of López Obrador’s ruling Morena party late last month that Mexico “will never kneel before economic power, nor before any foreign power.”

Polls show there is widespread dissatisfaction with the corruption-riddled Mexican justice system, but many think the judicial overhaul is a giant step backward, ending the progress the country has made toward an open economy and a multiparty democracy. Former President Ernesto Zedillo , who played a key role in Mexico’s democratic transition in 2000, said the changes mark the end of an era. The measures “will bury Mexican democracy and what remains of its fragile rule of law,” he said in a speech last month.

The reorganization of the judiciary is likely to be a significant distraction from other pressing issues. These include an economic slowdown and a spiraling crisis as violent drug gangs fight for control of swaths of Mexico. Sheinbaum lacks the charismatic personality of López Obrador, whose folksy style and daily news conferences enabled him to establish a personal link with many Mexicans as he doubled the minimum wage and expanded cash handouts to millions.

The white-haired departing president has said he would retire from politics to his hacienda in southern Mexico, but many believe that he will remain as the power behind the presidential chair . Presidents can’t run for re-election in Mexico.

Serious and data-driven, Sheinbaum holds a doctorate in environmental engineering. She also took graduate-level courses on energy efficiency at Stanford University and the University of California at Berkeley. Known as a tough taskmaster, her success will depend on her ability to deliver results rather than on personal charisma, said a person close to her governing team.

Sheinbaum grew up in a leftist academic household in Mexico City where she learned French, took ballet lessons, and cut her political teeth on university activism. She joined López Obrador as Mexico City’s environmental chief when he was the city’s mayor, supervising construction of an elevated highway, a key infrastructure project.

Since then, she has been unquestionably loyal to López Obrador. Rarely straying from under his shadow, Sheinbaum remains an enigma to many, including U.S. officials who hope her government will be marked by less domestic polarization and greater bilateral cooperation than under López Obrador.

Security cooperation is set to expand under Sheinbaum, U.S. and Mexican officials say. Her designated Security Minister Omar García Harfuch has a long record of cooperation with U.S. counterparts . At the top of the agenda with the U.S. is identifying and capturing top leaders of Mexico’s fentanyl smuggling gangs , while Mexico wants to take down the most violent organizations.

Mexican and U.S. officials are considering setting up a vetted unit within Mexico’s National Guard that will be screened for organized crime ties and receive training in the U.S., these people said. These units would be formed by designated law-enforcement officials to investigate and capture drug bosses and migrant smugglers.

Gen. Ricardo Trevilla, who was appointed by Sheinbaum as defense minister, is also expected to take a tougher stance against drug trafficking, said retired Gen. Tomás Ángeles, who served as Mexico’s deputy defense minister.

“I expect more bullets and fewer hugs,” Ángeles said, referring to López Obrador’s motto of tackling criminal activity with “ hugs not bullets .”

Write to José de Córdoba at jose.decordoba@wsj.com and Santiago Pérez at santiago.perez@wsj.com