1

Is it possible to change IDs columns into two linked pandas dataframes?

Example:

I have these two dataframes: Users and Ratings enter image description here

I would like the Users IDs to become as follows: enter image description here

This is my original code:

import pandas as pd
my_dict1 = {'id_user': [-88125, 1256, 1889, -17777, 123, 40506, -885556], 'Age': [20, 32, 45, 20, 21, 33, 54]}
users = pd.DataFrame(my_dict1)
print('******************Users************************')
print(users)
print('******************Ratings************************')
my_dict2 = {'id_user': [-88125, 88125, 1889, 123, 123, -885556, -885556], 'idMovie': [14, 22, 14, 17, 20, 1, 8], 'Rating': [5.0, 1.0, 2.0, 4.0, 3.0, 5.0, 4.0]}
Ratings = pd.DataFrame(my_dict2)
print(Ratings)
Driss AL
  • 11
  • 2

1 Answers1

0

Please follow the below steps.

  1. Create a new column called user_id in the Users dataframe and fill values 1,2,3....
  2. Iterate the Ratings dataframe row by row and search for the id_user in the id_user column in the Users dataframe, if we find a match take the value from the newly introduced column user_id and replace the id_user column in the Ratings dataframe.

If required i can try the logic and share. Thanks