用数据框中的单词替换列表中的单词,Python

时间:2014-02-13 03:51:15

标签: python for-loop replace pandas dataframe

我有一个句子列表,我需要用数据框列中的单词替换每个单词。这是一对一的通信。这是我的代码:

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

最终给出错误“无法分配给函数调用”。这就是数据的样子:

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

现在计算z,给我:

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

dataframe是一个系列(类似于对象的对象),它有一列,每行包含一个单词,如下所示:enter image description here

所以现在我需要用图像中的这些单词(正确的单词)替换z中的单词,即数据帧..我该怎么做?

1 个答案:

答案 0 :(得分:2)

你正在使用str.replace错误。您应该在字符串上调用它,并指定要替换的子字符串和要替换它的新字符串。它的工作原理如下:

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

至于你的实际程序,从你的描述看来你真的想要这样的东西:

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

那将输出:

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']

虽然我必须承认,这似乎毫无意义,因为结果与data frame中的结果完全相同。也许你可以进一步澄清。