2016-12-05 33 views
-1

我有一個DF如果「道」在A列中,添加一個「字」,以一個空列「B」

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

如果超過一個字是什麼禮物? –

+0

和'df'是一個列表,元組,字典,文件..? –

回答

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) 
相關問題