Reputation: 372
I've factorized my previous dataframe using this code
df['name'], mapping = df['name'].factorize()
Now I am trying to get the same factorized value for my new dataframe using the variable mapping
Mapping
Index(['Ranny','Bob','Timmy','Lisa'],
dtype='object')
My new dataframe
╔════════════════╗
║ Name Apples ║
╠════════════════╣
║ Lisa 5 ║
║ Timmy 3 ║
║ Lisa 2 ║
║ Bob 1 ║
╚════════════════╝
The dataframe I want after mapping the column name to the list
╔════════════════╗
║ Name Apples ║
╠════════════════╣
║ 3 5 ║
║ 2 3 ║
║ 3 2 ║
║ 1 1 ║
╚════════════════╝
I'm not sure how to do it. I tried to a loop but it takes too much time when the dataset is big. Is there an efficient way to do this?
Upvotes: 1
Views: 58
Reputation: 75080
If I understand correctly , you can use pd.Index.get_Indexer
here:
df2['Name'] = mapping.get_indexer(df2['Name'])
print(df)
Name Apples
0 3 5
1 2 3
2 3 2
3 1 1
Upvotes: 3