为csv fi中的每一行生成新输出

2024-04-25 15:09:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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

我想循环遍历每一行并为每一行生成新的输出,如下所示

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


Tags: keypostypenpwheresystemexactpd