0
votes

i have a list of sentences and i need to replace each word by a word from a data frame column. it is a one to one correspondence. here's my code:

def replace_words(x):
    z = [word for line in x for word in line.split()]
    for i in range(0 ,(len(z)-1)):
    z[i] = str.replace(z[i],dataframe[i])
    return z

which eventually gives an error "cant assign to function call" . this is how the data looks like:

x = ['I love flappy brd' , 'i got a platium medal','i hammred my fone']  .

now computing z, gives me:

z = ['I','love','flappy','brd','i','got','a','platium','medal','i','hammred','my','fone']

dataframe is a series(table like object) which has one column , and each row consists of 1 word it looks like this:enter image description here

so now i need to replace words in z with these words(correct ones)in image i.e dataframe .. how do i do that?

1
Do you want square brackets in the line z[i] = z[i].replace()? - TomAugspurger
yes i did that.. now the problem is with this error - "replace takes atleast 3 arguments(2 given)" . check it out, i've edited my code.. - Hypothetical Ninja

1 Answers

2
votes

You're using str.replace wrong. You should be invoking it on a string, and specifying a substring to replace and a new string to replace it with. It works like this:

>>> x = "hello world"
>>> x.replace("hello", "goodbye")
'goodbye world'

As for your actual program though, it seems from your description that you actually want something like this:

def replace_words(x, dataframe):
  z = [word for line in x for word in line.split()]
  for i in range(0, len(z)):
    z[i] = dataframe[i]
  return z

dataframe = ["I", "love", "flappy", "bird", "i", "got", "a", "platinum", "medal", "i", "hammered", "my", "phone"]
x = ['I love flappy brd' , 'i got a platium medal','i hammred my fone']
print "x was: " + str(x)
print "now: " + str(replace_words(x, dataframe))

That will output:

x was: ['I love flappy brd', 'i got a platium medal', 'i hammred my fone']
now: ['I', 'love', 'flappy', 'bird', 'i', 'got', 'a', 'platinum', 'medal', 'i', 'hammered', 'my', 'phone']

Though I must admit, that seems pointless, since the result is exactly the same as what is in data frame. Perhaps you can further clarify.