1

I am trying to compare values in 2 columns of a dataframe from the input and check for equality:

My input is a list like:

ABC BCD
QWE XYZ
MNO PQR

My dataframe looks like:

St1  St2

ABC  BCD
PQR  XYZ
MNO  PQR

I want to check of the input is present in my dataframe. And if yes, then corresponding to each of the values, I append a "Yes" or "No" on the basis of whether it is present or not.

I am trying to do this:

for i in range(len(pairs)):
    if df_final['Stock1']==pairs[i][0]:
        df["corr"] = "Yes"

This doesnt work and give the following error:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How can it be rectified?

I have tried this as well:

k = pairs[0]
df[(df[['St1','St2']].values == k).all(axis=1)]

where pairs is the input

4
  • What's in pairs? Commented Apr 16, 2022 at 22:32
  • @NYCCoder The input in the question is there in the pairs Commented Apr 16, 2022 at 22:35
  • Ok then what is in df_final. Can you paste a sample of that? Commented Apr 16, 2022 at 22:36
  • @NYCCoder The dataframe is the df_final with columns St1 and St2. I want 1 column with a yes or no if they exist i have also added 1 more approach. Though it doesnt work when run for all tuples in the list of input Commented Apr 16, 2022 at 22:38

1 Answer 1

1

This could be a solution for you based on this post. However, you need to make the column names consistent in both data sets:

import pandas as pd
import numpy as np


df = pd.merge(df1, df2, on=['St1','St2'], how='left', indicator='Exist')
df1['corr'] = np.where(df['Exist'] == 'both', True, False)
df1

   St1  St2   corr
0  ABC  BCD   True
1  QWE  DAS  False
2  MNO  PQR   True

Data

df1 = pd.DataFrame({'St1': ['ABC', 'QWE', 'MNO'], 'St2': ['BCD', 'DAS', 'PQR']})
df2 = pd.DataFrame({'St1': ['ABC', 'PQR', 'MNO'], 'St2': ['BCD', 'XYZ', 'PQR']})
Sign up to request clarification or add additional context in comments.

Comments

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.