Home > Software design >  Compare and match two data frames with multiple criteria
Compare and match two data frames with multiple criteria

Time:12-29

Given a dataframe

multival    date          string    others
23|34|45    12/05/1991    name1     xyz
31|46|25    16/02/1990    name2     abc

how can I create an index and lookup in above data series using values from another data series such that it match on

  • value in multival
  • date
  • string

Example: lookup on 23, 12/05/1991 and name1 should yield first data series

CodePudding user response:

sorry, but your question is not clear what exactly is needed: creating an index or just looking up data in one DF based on values from another DF's columns. If it is the latter, you can easily do it by creating filter conditions (masks) and applying them to your DF:

a = pd.DataFrame(
    {
        "date": ['12/05/1991', '16/02/1990'],
        "string": ['n1', 'n2'],
        "others": ['x', 'y'],
    }
)


b = pd.DataFrame(
    {
        "date": ['01/01/1991', '16/02/1990'],
        "string": ['n1', 'n2'],
        "others": ['xx', 'yy'],
    }
)

>>> a
         date string others
0  12/05/1991     n1      x
1  16/02/1990     n2      y
>>> b
         date string others
0  01/01/1991     n1     xx
1  16/02/1990     n2     yy


filters = a["date"].isin(b["date"]) & a["string"].isin(b["string"])
a[filters]
>>> a[filters]
         date string others
1  16/02/1990     n2      y

CodePudding user response:

l=[]

for j in range(len(df['A header'])):
    l.append(df['A header'][0][j])

 df['A header']=l 

out :

 A header        date string others
0        23  12/05/1991     n1      x
1        34  16/02/1990     n2      y
  • Related