Using nn.CrossEntropyLoss or nn.NLLLoss there is no point in creating one-hot encoded targets.
However if the targets are already stored as such, you could use the code snippets from this thread to get the class indices.
I’m not completely sure what your code snippet does exactly, but if you get the class index for each corresponding sample, it should work fine.
However, I’m not sure if pd.unique sorts the data or not. Sorting might destroy the data to target mapping, so you should check it.