I was taught in school that the rare earth metals were the $\mathrm{f}$-orbital group. Additionally, the Aufbau principle states that the order of orbitals based on energy levels is $\mathrm{6s4f5d}$ and $\mathrm{7s5f6d}$ for the lanthanides and actinides, respectively. However, upon inspection, both periods have 15 elements whereas the $\mathrm{f}$-orbitals can only hold 14 electrons. I was able to resolve this by assigning one electron to the $\mathrm{5d/6d}$-orbital, which is correct according to chemistry libretexts. Why does this occur for the rare earth metals?

Furthermore, according to libretexts, only some of the rare earth metals have a $\mathrm{5d/6d}$-orbital, whereas others have the $\mathrm{6s/7s}$-orbital and proceed to only fill their outermost $\mathrm{f}$-orbital. Why is this the case, and is there a way to determine which elements are like this short of simple memorization?