如果列A中的“Word”,则将“Word”添加到空列“B”

时间:2016-12-05 08:49:17

标签: python dataframe preprocessor

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

"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" 

1 个答案:

答案 0 :(得分:0)

你可以这样做:

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)