我有一个包含一些数据的csv文件,
match_type keyword campaign group
exact find a key Non-Branded Find key - Exact
phrase key physicians Branded System (key) key Brand (Acronyn) - Phrase
phrase key locations Branded System (key) key Brand (Acronyn) - Phrase
我想遍历每一行,并为每个match_type
生成新的输出,就像这样,
exact = pd.DataFrame()
phrase = pd.DataFrame()
for x in match_type:
if x == 'exact':
exact['Match type'] = pd.np.where(pos.Match_type.str.contains('exact'), 'exact',
pd.np.where(pos.Match_type.str.contains('phrase'), 'negative exact', 'negative exact'))
exact['Keyword'] = pd.np.where(pos.Search_term.str.contains('physicians'), 'key physicians',
pd.np.where(pos.Search_term.str.contains('locations'), 'key locations',
pd.np.where(pos.Search_term.str.contains('find'), 'find a key', 'None')))
exact['Campaign'] = pd.np.where(pos.Campaign.str.contains('Branded'), 'Branded System (key)',
pd.np.where(pos.Campaign.str.contains('Non-Branded'), 'Non-Branded Brand', 'None'))
exact['Ad_Group'] = pd.np.where(pos.Ad_group.str.contains('- Phrase'), 'key Brand (Acronyn) - Phrase',
pd.np.where(pos.Ad_group.str.contains('- Exact'), 'Find key - Exact', 'None'))
elif x == 'phrase':
phrase['Match type'] = pd.np.where(pos.Match_type.str.contains('exact'), 'negative phrase',
pd.np.where(pos.Match_type.str.contains('phrase'), 'phrase', 'negative phrase'))
phrase['Keyword'] = pd.np.where(pos.Search_term.str.contains('physicians'), 'key physicians',
pd.np.where(pos.Search_term.str.contains('locations'), 'key locations',
pd.np.where(pos.Search_term.str.contains('find'), 'find a key', 'None')))
phrase['Campaign'] = pd.np.where(pos.Campaign.str.contains('Branded'), 'Branded System (key)',
pd.np.where(pos.Campaign.str.contains('Non-Branded'), 'Non-Branded Brand', 'None'))
phrase['Ad_Group'] = pd.np.where(pos.Ad_group.str.contains('- Phrase'), 'key Brand (Acronyn) - Phrase',
pd.np.where(pos.Ad_group.str.contains('- Exact'), 'Find key - Exact', 'None'))
现在这有效,它遍历每一行并将数据转换为所需的输出。看起来像
Match type Keyword Campaign Ad_Group
negative exact key physicians Branded System (key) key Brand (Acronyn) - Phrase
negative exact key locations Branded System (key) key Brand (Acronyn) - Phrase
exact find a key Non-Branded Find key - Exact
phrase key physicians Branded System (key) key Brand (Acronyn) - Phrase
phrase key locations Branded System (key) key Brand (Acronyn) - Phrase
negative phrase find a key Non-Branded Find key - Exact
我在这里遇到的麻烦是对代码进行泛化,因此它适用于包含不同数据的任何csv。 match_type
将保持不变,但所有其他列输入可以不同。我似乎找不到任何可以帮助我概括这一点的东西。也许是因为我对编程还很陌生
变化数据的示例,
match_type keyword campaign group
exact other key Non-Branded only Other key - Exact
exact key Branded Brand - Exact
phrase other key locations System (key) key Brand - Phrase
exact key Branded Brand - Exact
phrase key locations only System (key) key Brand - Phrase