如果“单词”在列 A 中,将该“单词”添加到空列“B”

If “Word” In Column A, Add that “Word” to An Empty Column “B”

我有一个DF

Weather_opinion:

"the weather is cold"      
"it's quite sunny today"      
"it's raining"         
"today the climate seems to be pleasant"      
"I feel dizzy today"

还有一个列表:

list_tags = ["cold", "sunny" "raining" "pleasant", "hot", "dry"]

所以,如果标签存在于 DF 的一行中,我想添加另一列,带有标签。如果 DF 中不存在列表中的单词,请添加一些通用标签。

所以,新的输出将是

Weather_opinion ~ 标签:

"the weather is cold" ~ "cold"      
"it's quite sunny today" ~ "sunny"      
"it's raining" ~ "raining"       
"today the climate seems to be pleasant" "pleasant"     
"I feel dizzy today" ~ "others" 

你可以这样做:

DF = [
    "the weather is cold",
    "it's quite sunny today",      
    "it's raining",
    "today the climate seems to be pleasant",      
    "I feel dizzy today"
]

list_tags = ["cold", "sunny" "raining" "pleasant", "hot", "dry"]

for option in DF:
    found_tag = "others"
    for tag in list_tags:
        if tag in option:
            found_tag = tag
            break
    print(option, found_tag)