2016-12-05 7 views
-1

Ich habe eine DFWenn „Wort“ in Spalte A, hinzufügen, dass „Wort“ auf eine leere Spalte „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" 

Und eine Liste:

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

Wenn also ein Tag in einer Zeile des DF vorhanden ist, möchte ich eine weitere Spalte mit dem Tag hinzufügen. Wenn kein Wort aus der Liste im DF vorhanden ist, fügen Sie ein generalisiertes Tag hinzu.

So würde die neue Ausgabe

Weather_opinion sein ~ 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" 
+2

Was passiert, wenn mehr als ein Wort vorhanden ist? –

+0

Und 'df' ist eine Liste, Tupel, Diktat, Datei ..? –

Antwort

0

Sie können es wie folgt aus:

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) 
Verwandte Themen