python_newbie
python_newbie

Reputation: 1

Check if column values exists in different dataframe

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?

Upvotes: 0

Views: 336

Answers (1)

Manjunath K Mayya
Manjunath K Mayya

Reputation: 1118

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

Upvotes: 1

Related Questions