0

Assume I have a data frame as follows:

enter image description here

I have another data frame with 2 million rows where the user id in that data frame contains only the elements of list 1. I want to change anytime I see something from list 1 to the corresponding element in list 2. What would be an efficient way to achieve this: I can loop over the second data frame lets say df2, and do something like this:

for ii, user in enumerate(df2['user_id]):
    for jj,L in enumerate(df['list1']):
       if(user == L):
         df2['user_id'].iloc[ii] = df['list2'].iloc[jj]

But this looks very inefficient and looks to be O(n^2) complexity since currently, df might look to have 20 rows, but it can have 1 million rows. I am wondering if there is an easier way to achieve this by using a map or apply or any other methods.

user59419
  • 893
  • 8
  • 20

0 Answers0