【问题标题】:Iterate through a list of strings from a column in dataframe python遍历数据框python中列中的字符串列表
【发布时间】:2021-05-16 02:51:44
【问题描述】:

我有一个 pandas 数据框,其中包含“消息”列中的单词列表。如何遍历列中的字符串列表以应​​用我创建的函数来纠正拼写?

我已经准备好修正功能了。

def correct_spelling(data):
w = Word(data) 
correct_word = (w.correct())
return correct_word



df['Message'] = correct_spelling(df['Message'])

初始数据帧

S/No 月份消息

6 月 0 日 [嘿,哇,你,你,在做,今天,我...]

8 月 1 日 [sally,认为,那个,这个,jobb,是,easyy...]

2 月 2 日 [try, to,buy, him, a, new, watch...]

12 月 3 日 [i, have, twp, much, taime, on, my, hand...]

最终数据帧

S/No 月份消息

0 June [嘿,你好吗,今天,我...]

8 月 1 日 [莎莉,认为,这,这个,工作,很容易......]

2 月 2 日 [尝试、购买、购买、他、a、新、手表...]

12 月 3 日 [i, have, two, much, time, on, my, hand...]

【问题讨论】:

  • df['Message'] = df['Message'].apply(lambda x: correct_spelling(x)) ?
  • 我收到以下错误。 TypeError: 'in ' 需要字符串作为左操作数,而不是列表
  • 没有注意到你的消息是一个列表,df['Message'] = df['Message'].apply(lambda x: [correct_spelling(y) for y in x]),检查 Jezrael 的答案

标签: python pandas dataframe


【解决方案1】:

如果需要单独处理每个单词使用:

df['Message'] = df['Message'].apply(lambda x: [correct_spelling(y) for y in x])

如果可能通过名单:

df['Message'] = df['Message'].apply(correct_spelling)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-17
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2022-07-07
    相关资源
    最近更新 更多