It's true that only Liberia and Ethiopia existed as large independent states during Age of Imperialism in Africa. Liberia's independence through this period had to do with its origins. It was founded by expatriated free men of color from the United States. This was part of the efforts of the American Colonization Society, which argued for emancipation and deportation of enslaved people. As thousands of American expatriates moved to the region, they established, with the support of the American and British governments, a nation-state, which the United States reluctantly recognized in the 1860s. In the years that followed, the United States essentially guaranteed, at least in principle, Liberian independence amid the "Scramble for Africa" among European powers. For this reason, Liberia remained independent throughout the nineteenth and twentieth centuries.
The nation-state of Ethiopia, on the other hand, was not of colonial origins. Rather, a long power struggle between rival factions in that country ended with efforts to create a centralized nation-state in response to the threat of European incursion and colonialism. That said, the kingdom, which had ancient origins, was under threat of colonization by Italy during much of the period. The European nation, desperate to stake a claim alongside France, Great Britain, and Germany as a colonial power, declared a protectorate over Ethiopia and attempted to establish military and political control over the kingdom in the late nineteenth century. But in 1896, Ethiopian forces under Menelik II, the emperor of a newly unified nation-state, inflicted a humiliating defeat on Italian forces at the Battle of Adowa. This Ethiopian victory preserved the independence of Ethiopia until 1935, when Benito Mussolini, hoping to avenge this old affront to Italian national pride, ordered the invasion of the kingdom.