[英]how to save output of if elif statement to new variable in python dataframe?
how do i edit the following script to save the outputs as new variables in the original dataframe? 如何编辑以下脚本以将输出保存为原始数据框中的新变量?
AKA: instead of the print function, have the output be saved as a new variable for each if elif statement? AKA:将输出保存为每个if ifif语句的新变量,而不是print函数?
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}")
ie, : new_model_core_incentives
or used_model_core_incentives
as a new variable in the dataframe and new_word
and old_word
as a new variable in the dataframe? 即: new_model_core_incentives
或used_model_core_incentives
作为数据帧中的新变量, new_word
和old_word
作为数据帧中的新变量?
You could use a dictionary: 你可以使用字典:
dict[Keyword]=f"new_model_core_incentives {new_model_core_incentives}"
dict2[Keyword]=f"old_word {old_word}"
Something like this: 像这样的东西:
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}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.