0

I have two dataframes and am trying to update Name in DF1 based on two conditions Tool and Location-

Dataframes -

first one

DF1

NAME    Tool    Location
-       tool_1  location_1
-       tool_15 location_2 
-       tool_19 location_3 

and second one -

DF2

NAME    Tool    Location
name51  tool_1  location_1
name42  tool_15 location_2 
name33  tool_19 location_3

I've tried using a numpy where condition checking for both values however am getting error that states -

ValueError: Can only compare identically-labeled Series objects

I understand the problem is different row numbers in both of my dataframes. I've tried some solutions with resetting indexes without any success.

Here is my attempted query -

DF1['NAME'] = np.where((DF1.Tool == DF2.Tool) & (DF1.Location== DF2.Location), DF2.Name)

What could be a work around for this problem ? I am unable to match both dataframes with the exact length of rows.

Expected result for DF1 would be -

DF1

NAME    Tool    Location
name51  tool_1  location_1
name42  tool_15 location_2 
name33  tool_19 location_3

thank you,

1 Answer 1

1

Merge and reindex():

DF1=DF1.merge(DF2,on=['Tool','Location'],suffixes=('_x','')).reindex(DF1.columns,axis=1)

     NAME     Tool    Location
0  name51   tool_1  location_1
1  name42  tool_15  location_2
2  name33  tool_19  location_3
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for the input! How would you work around if lets say my query would look like this - DF1['NAME'] = np.where((DF1.Tool == DF2.Tool) & (DF1.Location.isin(DF2.Location)), DF2.Name) . I have more string columns that need to be matched but they are not always exactly the same and only first 2 characters match
@PaulGirtavic the problem is this will look index wise, lets say tool15 is over tool_1, this will give false, so merge is better option here
@PaulGirtavic you can also try df1['NAME']=np.where(df1.Tool.eq(df2.Tool)&(df1.Location.isin(df2.Location)),df1.Tool.map(df2.set_index('Tool')['NAME']),df1.NAME) but i will still prefer merge solution here

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.