-2

It's a very basic question but somehow my loop does not work. I have a pandas dataframe with two columns; path and word. A neural network predicted the outcome value in word, however this is still an integer. I wrote a for loop to replace those integers with words but there are no changes in the df. My df:

path    word
0   f7f172c01bec6a3e9a36dcafdf9e02e7df3522e4.wav    21
1   c17c29e392c57a9243ed52175568b40c04912194.wav    21
2   eea9239d4986b1f7dbffdcce76e0fce6e5f38ca8.wav    21
3   4fec4b033ba19d1ac875c0c062fda2869dbece73.wav    21

my loop:

for i in df['word']:
    if i == 0:
        i == "backward"
    elif i == 1:
        i == "bed"
    elif i == 2:
        i == "bird"
    elif i == 3:
        i == "cat"
    elif i == 4:
        i == "dog"
    elif i == 5:
        i == "down"
    elif i == 6:
        i == "eight"
    elif i == 7:
        i == "five"
    elif i == 8:
        i == "follow"
    elif i == 9:
        i == "forward"
    elif i == 10:
        i == "four"
    elif i == 11:
        i == "go"
    elif i == 12:
        i == "happy"
    elif i == 13:
        i == "house"
    elif i == 14:
        i == "learn"
    elif i == 15:
        i == "left"
    elif i == 16:
        i == "marvin"
    elif i == 17:
        i == "nine"
    elif i == 18:
        i == "no"
    elif i == 19:
        i == "off"
    elif i == 20:
        i == "on"
    elif i == 21:
        i == "one"
    elif i == 22:
        i == "right"
    elif i == 23:
        i == "seven"
    elif i == 24:
        i == "sheilla"
    elif i == 25:
        i == "six"
    elif i == 26:
        i == "stop"
    elif i == 27:
        i == "three"
    elif i == 28:
        i == "tree"
    elif i == 29:
        i == "two"
    elif i == 30:
        i == "up"
    elif i == 31:
        i == "visual"
    elif i == 32:
        i == "wow"
    elif i == 33:
        i == "yes"
    elif i == 34:
        i == "zero"
Eeuwigestudent1
  • 161
  • 1
  • 2
  • 13
  • Please provide [minimal code to reproduce the issue](https://stackoverflow.com/help/how-to-ask) and indicate what exactly you want to achieve. – sophros Dec 14 '18 at 18:13
  • My opinion is that you need to read about how to use pandas before even using it for complex scenarios such as neural network. As the answer mentions, map should work. – Vishnudev Krishnadas Dec 14 '18 at 18:16

1 Answers1

2

You can use pd.Series.map:

#Add the rest of your mapping here (I just included a few)
mapping = {0: 'backward', 1: 'bed', 21: 'one', 22: 'right'}

df['word'] = df['word'].map(mapping)

Returns:

                                           path word
0  f7f172c01bec6a3e9a36dcafdf9e02e7df3522e4.wav  one
1  c17c29e392c57a9243ed52175568b40c04912194.wav  one
2  eea9239d4986b1f7dbffdcce76e0fce6e5f38ca8.wav  one
3  4fec4b033ba19d1ac875c0c062fda2869dbece73.wav  one
rahlf23
  • 8,869
  • 4
  • 24
  • 54