We study the electronic structure, formation energies, and magnetic properties of cementite Fe 3 C doped by 3d and 4d transition metals (TM) by means of first-principles calculations. All TM elements demonstrate a preference for the substitution of the general iron position in Fe 3 C. We predict that early elements (Sc, Ti, V, Cr, Zr, and Nb) stabilize the cementite, while the end elements (Ni, Cu, Pd, and Ag) in the TM series sharply destabilize it. The magnetic properties of impurity atoms are found to depend strongly on their atomic numbers and the trends to ferromagnetic or antiferromagnetic coupling of impurities with Fe atoms in Fe 3 C coincide with the magnetic behavior of these impurities in Fe. The physical reason is a similar location of the Fermi level in the pseudogap of spin-down and almost filled spin-up Fed states in both Fe 3 C and Fe.