У меня есть 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