Home > Mobile >  Compare columns of two dataframes with custom functions
Compare columns of two dataframes with custom functions

Time:01-27

Given the following two dataframes:

df1 = pd.DataFrame(data={'unicorn': ['blue', 'red', 'piNk'], 'size': [3, 4, 6]})
df2 = pd.DataFrame(data={'unicorn': ['red'], 'size': [2]})

df1:

  unicorn  size
0    blue     3
1     red     4
2    piNk     6

df2 (always has one row):

  unicorn  size
0     red     2

How can I compare the rows of both dataframes column-wise using custom comparison functions like this (simplified):

def unicorn_comparison(str1, str2) -> float:
  return 100.0 if str1 == str2 else 0.0

and

def size_comparison(nr1, nr2) -> float:
  return 100.0 if nr1 < nr2 else 0.0

Expected result:

   unicorn  size
0      0.0   0.0
1    100.0   0.0
2      0.0   0.0

CodePudding user response:

As you have always a single row in df2, don't use a DataFrame (2D), but a Series (1D).

ser = df2.loc[0]

Then assuming you just want a comparison, use vectorial code (not a custom function):

out = df1.eq(ser)*100

If you really need to use a non-vectorial function and have to compare all combinations, use:

def unicorn_comparison(str1, str2) -> float:
    return 100.0 if str1 == str2 else 0.0

def size_comparison(nr1, nr2) -> float:
    return 100.0 if nr1 < nr2 else 0.0

funcs = {'unicorn': unicorn_comparison,
         'size': size_comparison
        }

out = df1.apply(lambda c: c.apply(lambda s: funcs[c.name](s, ser[c.name])))

output:

   unicorn  size
0        0     0
1      100     0
2        0     0

CodePudding user response:

its way. first; add df2's column of you want.

df1['unicorn2'] = df2['unicorn']

after; You can use "application loop". You can run the logic of the you want in the "application loop".

def function(x):
    # your logic
  return x
df1_result = df1.apply(function)

CodePudding user response:

for col in df1:
    df1[col] = (df1[col] == df2[col].loc[0]).replace({True: 100, False: 0})

This will overwrite your df1, or you can make a copy of it first.

  •  Tags:  
  • Related