基于原始DF(Python 3,Pandas)对列表中的值进行分类

时间:2015-02-25 21:57:34

标签: python-3.x pandas

虚构的名为KW的df看起来像这样:

 Group    Subgroup    Word
 orange    zebra      keys
 green     lion       mouse
 blue      horse      captain

我目前的代码会在" Word"下找到每个单词。列和一次一个字典中的其他字母替换某些字母。在此之后,创建所有这些拼写错误的列表。所以使用KW df:

kw = df[['Word',"Group","Subgroup"]]

words = kw.to_dict()["Word"].values()

md = {"m":"w","o":"z"}
md = {k: v.split(',') for k, v in md.items()}

newwords = []

for word in words:
    newwords.append(word)
    for c in md:
        occ = word.count(c)
        pos = 0
        for _ in range(occ):
            pos = word.find(c, pos)
            for r in md[c]:
                tmp = word[:pos] + r + word[pos+1:]
                newwords.append(tmp)
            pos += 1

返回

Word
keys
mouse
wouse
mzuse
captain

我想要做的事情基本上是根据操作的原始单词将这些拼写错误重新分类到相应的组/子组中。理想情况下,它不是单独列出拼写错误列表,而是看起来像这样:

Group    Subgroup    Word
orange   zebra       keys
green    lion        mouse
green    lion        wouse
green    lion        mzuse
blue     horse       captain

1 个答案:

答案 0 :(得分:1)

不知何故,我们需要将新单词与原始单词相关联。 您可以通过存储newwords 2元组(例如('mouse', 'wouse'))来实现。 然后,您可以将newwords转换为DataFrame,并使用pd.mergenewwordskw合并,方法是加入原始字词:

import pandas as pd

df = pd.read_table('data', sep='\s+')
kw = df[['Word',"Group","Subgroup"]]

words = df['Word']

md = {"m":"w","o":"z"}
md = {k: v.split(',') for k, v in md.items()}

newwords = []
for word in words:
    # Save both the original word and the new word
    newwords.append((word, word))
    for c in md:
        occ = word.count(c)
        pos = 0
        for _ in range(occ):
            pos = word.find(c, pos)
            for r in md[c]:
                tmp = word[:pos] + r + word[pos+1:]
                newwords.append((word, tmp))

newwords = pd.DataFrame(newwords, columns=['Word', 'New'])
# Merge on the original Word
result = pd.merge(newwords, kw, left_on='Word', right_on='Word', how='left')
result = result[['Group', 'Subgroup', 'New']]
result.columns =  ['Group', 'Subgroup', 'Word']
print(result)

产量

    Group Subgroup     Word
0  orange    zebra     keys
1   green     lion    mouse
2   green     lion    wouse
3   green     lion    mzuse
4    blue    horse  captain