我试图找到一种方法来合并多列在同一时间与Pandas。我有我想要的输出做五个单独的合并,但感觉应该有一个更Python的方式来做。
实际上,我有一个名为df_striking的 Dataframe ,其中包含五个关键字列,我试图将另一个 Dataframe (名为df_keyword_vol)中的搜索卷数据合并到相邻的行中。
最小重现性示例:
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)
所需输出
我已经尝试过了。我已经做了两个函数来合并关键字数据一次一行,但它只是不是很Python!
# 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")
3条答案
按热度按时间ojsjcaue1#
如果已经有空列,则可以用途:
否则,如果只有
KWx
列:输出:
6gpjuf902#
如果您将其全部转换为长格式,则会更容易:
然后,您可以使用
.pivot
获得原始格式,但使用多索引作为列:我们可以使用
pd.concat
来解决这种奇怪的格式:r3i60tvu3#