как сохранить вывод оператора if elif в новую переменную в python dataframe? - PullRequest
0 голосов
/ 24 марта 2019

как мне отредактировать следующий скрипт, чтобы сохранить выходные данные как новые переменные в исходном кадре данных?

AKA: вместо функции печати выходные данные сохраняются в виде новой переменной для каждого оператора if elif?

import re

df = pd.read_excel('edmundstest.xlsx')

for Keyword, Landing_Page in zip(df["Keyword"], df["Landing_Page"]):

# the url
    if "/2019/" in Landing_Page:
        new_model_core_incentives = Landing_Page
        print(f"new_model_core_incentives {new_model_core_incentives}")
    elif re.search("/(?:(?:20)|(?:19))\d{2}/", Landing_Page):
        used_model_core_incentives = Landing_Page 
        print(f"used_model_core_incentives {used_model_core_incentives}")    

# the "keywords"
    if "2019" in Keyword:
        new_word = Keyword
        print(f"new_word {new_word}")
    elif re.search("/(?:(?:20)|(?:19))\d{2}/", Keyword) is None:
        old_word = Keyword
        print(f"old_word {old_word}")

т.е.: new_model_core_incentives или used_model_core_incentives в качестве новой переменной в кадре данных и new_word и old_word в качестве новой переменной в кадре данных?

1 Ответ

0 голосов
/ 24 марта 2019

Вы можете использовать словарь:

dict[Keyword]=f"new_model_core_incentives {new_model_core_incentives}"
dict2[Keyword]=f"old_word {old_word}"

Примерно так:

import re

df = pd.read_excel('edmundstest.xlsx')

dict, dict2 = {}, {}

for Keyword, Landing_Page in zip(df["Keyword"], df["Landing_Page"]):

# the url
    if "/2019/" in Landing_Page:
        new_model_core_incentives = Landing_Page
        print(f"new_model_core_incentives {new_model_core_incentives}")
    elif re.search("/(?:(?:20)|(?:19))\d{2}/", Landing_Page):
        used_model_core_incentives = Landing_Page 
        dict[Keyword]=f"new_model_core_incentives {new_model_core_incentives}"    

# the "keywords"
    if "2019" in Keyword:
        new_word = Keyword
        print(f"new_word {new_word}")
    elif re.search("/(?:(?:20)|(?:19))\d{2}/", Keyword) is None:
        old_word = Keyword
        dict2[Keyword]=f"old_word {old_word}"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...