'Compare columns of two dataframes with custom functions
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
Solution 1:[1]
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)
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zendem |
| Solution 2 | Raymond Kwok |
