I'm trying to find a way to merge in multiple columns at the same time with Pandas. I have the output I want by doing five separate merges, but it feels like there should be a more pythonic way to do it.
Essentially I have a dataframe with five keyword columns in a dataframe called df_striking which I'm trying to merge in search volume data from another dataframe (called df_keyword_vol) into adjacent rows.
Minimum Reproducible Example:
import pandas as pd
striking_data = {
"KW1": ["nectarine", "apricot", "plum"],
"KW1 Vol": ["", "", ""],
"KW2": ["apple", "orange", "pear"],
"KW2 Vol": ["", "", ""],
"KW3": ["banana", "grapefruit", "cherry"],
"KW3 Vol": ["", "", ""],
"KW4": ["kiwi", "lemon", "peach"],
"KW4 Vol": ["", "", ""],
"KW5": ["raspberry", "blueberry", "berries"],
"KW5 Vol": ["", "", ""],
}
df_striking = pd.DataFrame(striking_data)
keyword_vol_data = {
"Keyword": [
"nectarine",
"apricot",
"plum",
"apple",
"orange",
"pear",
"banana",
"grapefruit",
"cherry",
"kiwi",
"lemon",
"peach",
"raspberry",
"blueberry",
"berries",
],
"Volume": [
1000,
500,
200,
600,
800,
1000,
450,
10,
900,
1200,
150,
700,
400,
850,
1000,
],
}
df_keyword_vol = pd.DataFrame(keyword_vol_data)
Desired Output
What I've tried. I've made two functions to merge the keyword data a row a time, but it's just not very pythonic!
# two functions to merge in the keyword volume data for KWs 1 - 5
def merger(col1, col2):
dx = df_striking.merge(df_keyword_vol, how='left', left_on=col1, right_on=col2)
return dx
def volume(vol1, vol2):
vol = df_striking[vol1] = df_striking[vol2]
df_striking.drop(['Keyword', 'Volume'], axis=1, inplace=True)
return vol
df_striking = merger("KW1", "Keyword")
volume("KW1 Vol", "Volume")
df_striking = merger("KW2", "Keyword")
volume("KW2 Vol", "Volume")
df_striking = merger("KW3", "Keyword")
volume("KW3 Vol", "Volume")
df_striking = merger("KW4", "Keyword")
volume("KW4 Vol", "Volume")
df_striking = merger("KW5", "Keyword")
volume("KW5 Vol", "Volume")

KWx Volin your dataframe?replaceon the odd columns, see my answer below