Search code examples
pythonpandasfunctiondataframerow

Create a new column with [0,1] based on match between two rows in Python


i am trying to compare multiple lists or dataframes to one large base dataframe. Then for any match i want to append a column storing 1 = Match or 0 = No Match

df = pd.DataFrame({'Name':['A','B','C','D'], 'ID' : ['5-6','6-7','8-9','7']})
list1 = ['5-6','8-9']
list2 = ['7','4-3']

As the values i am trying to match include a '-' they are counted as string. I can generate a list of matching values already, but if i append them, they are all 0

def f(rows):
    for i in df['ID']:
        for j in list1:
            if i == j:
                val = 1
            else:
                val = 0
            return val

df['Answer']= df.apply(f,axis=1)

While

for i in df['ID']:
    for j in  list1:
        if i == j:
           print (i)

Finds all matching values.

Thanks in advance!


Solution

  • You already loop by .apply, so you can omit loops and for test is use in for membership of list:

    def f(rows):
        if rows['ID'] in list1:
            val = 1
        else:
            val = 0
        return val
    
    df['Answer']= df.apply(f,axis=1)
    print (df)
      Name   ID  Answer
    0    A  5-6       1
    1    B  6-7       0
    2    C  8-9       1
    3    D    7       0
    

    Simplier is use lambda function with specify column:

    df['Answer']= df['ID'].apply(lambda x: 1 if x in list1 else 0)
    

    Or:

    df['Answer']= df['ID'].apply(lambda x: int(x in list1))