0

I have a pandas DataFrame 'df' with x rows, and another pandas DataFrame 'df2' with y rows (x < y). I want to return the indexes of where the values of df['Farm'] equals the value of df2['Fields'], in order to add respective 'Manager' to df.

the code I have is as follows:

data2 = [['field1', 'Paul G'] , ['field2', 'Mark R'], ['field3', 'Roy Jr']]
data = [['field1'] , ['field2']]
columns = ['Field']
columns2 = ['Field', 'Manager']
df = pd.DataFrame(data, columns=columns)
df2 = pd.DataFrame(data2, columns=columns2)

farmNames = df['Farm']
exists = farmNames.reset_index(drop=True) == df1['Field'].reset_index(drop=True)

This returns the error message:

ValueError: Can only compare identically-labeled Series objects

Does anyone know how to fix this?

4
  • That sounds like a merge. Is that what you're looking for? Commented Feb 27, 2022 at 4:05
  • Yes! I want to use a boolean series 'exists' to merge another column (say 'Manager') in df2, into df Commented Feb 27, 2022 at 4:08
  • Not sure what you mean - could you post two example input dataframes for df1 and df2, plus the output? They don't need to be big. In fact, the smaller the better as long as it shows the problem. Commented Feb 27, 2022 at 4:13
  • 1
    I edited the original question with your request Commented Feb 27, 2022 at 4:28

1 Answer 1

1

As @NickODell mentioned, you could use a merge, basically a left join. See below code.

df_new = pd.merge(df, df2, on = 'Field', how = 'left')
print(df_new)

Output:

    Field Manager
0  field1  Paul G
1  field2  Mark R
Sign up to request clarification or add additional context in comments.

1 Comment

oh wow, another reason to love python i guess. Thanks!

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.