I have 2 DataFrames in Python Pandas like below:
Input:
df1 = pd.DataFrame({"col1":["APPLE", "BANANA", "ORANGE"]})
df2 = pd.DataFrame({"col_x":["APPLEXX", "BANANA", "CARROT"]})
df1:
col1
------
APPLE
ORANGE
BANANA
df2:
col_x
--------
APPLEXX
BANANA
CARROT
Requirements:
And I need to print only rows from df2 (col_x), which: contain values from df1 (col1) as part or all of the value in df2 (col_x) and their equivalent in df1 (col1)
Desire output:
So, as an output I need something like below:
| col1 | col_x |
|---|---|
| APPLE | APPLEXX |
| BANANA | BANANA |
How can I do that in Python Pandas ?
You can take advantage of
assignment expressions(since python 3.8) and builtinnextmethod on generator expression:For older python versions use the following generator function: