0
$\begingroup$

I have a dataframe (namely 'original') with over a thousand numerical columns as features and one label column. I also have a shuffled version of the same dataframe (namely 'shuffled') with label column dropped. The question is, how to assign the correct labels to the second dataframe? I came up a with a ridiculous code that works, but surely there must be a simple straightforward way?

original.drop('label', axis = 1, inplace = True)

dic = dict()
for i in range(100000):
    dic[str(original.loc[i].tolist())] = label[i]

shuffled_labels = np.zeros(100000)
for i in range(100000):
    shuffled_labels[i] = dic[str(shuffled.loc[i].tolist())]
$\endgroup$

1 Answer 1

1
$\begingroup$

You should index the original dataframe by the shuffled one, as follows:

shuffled_labels = original.iloc[shuffled.index]['label']
shuffled_labels
$\endgroup$
3
  • $\begingroup$ Unfortunately this code provided a wrong answer. $\endgroup$
    – asmani
    Commented Oct 12, 2023 at 16:07
  • $\begingroup$ @asmani Probably depends on the way you obtain the shuffled dataset, you perform the shuffling using pandas? I ask because if the shuffled dataset has been re-indexed (i.e., by resetting the indices) then my answer won't work. $\endgroup$ Commented Oct 13, 2023 at 13:41
  • $\begingroup$ Yes, the shuffled dataset has been re-indexed, and that made it a challenge for me. $\endgroup$
    – asmani
    Commented Oct 14, 2023 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.