Панды - итерации по столбцу и обновление значения - PullRequest
0 голосов
/ 26 апреля 2018

Поскольку векторизатор tf-idf будет просто аварийно завершать работу всякий раз, когда он встречает новые метки, я пытаюсь удалить новые метки из моего нового ввода. Как я могу обновить значение столбца данных? Я делаю:

def clean_unseen(dfcol, vectorizer):
    cleanedstring = ""
    for entry in dfcol:
        for word in entry.split():
            if word in vectorizer.vocabulary_:
                cleanedstring = cleanedstring + " " + word
                print(cleanedstring)
        entry = cleanedstring
        cleanedstring = ""
    return dfcol

Пример:

tfifgbdf_vect= TfidfVectorizer()
s2 = pd.Series(['the cat', 'awesome xyz', 'f_g_h lol asd'])
tfifgbdf_vect.fit_transform(s2)
s3 = pd.Series(['the dog the awesome xyz', 'xyz lol asd', 'f_g_h lol aha'])
clean_unseen(s3, tfifgbdf_vect)

Это, однако, вернет исходный столбец без изменений:

Output: 
0    the dog the awesome xyz
1                xyz lol asd
2              f_g_h lol aha
dtype: object

1 Ответ

0 голосов
/ 26 апреля 2018

Поскольку отдельная запись в серии не является объектом, это всегда глубокая копия, а не ссылка, вам нужно явно изменить ее -

def clean_unseen(dfcol, vectorizer):
    dfc1 = []
    cleanedstring = ""
    for entry in dfcol:
        for word in entry.split():
            if word in vectorizer.vocabulary_:
                cleanedstring = cleanedstring + " " + word
                #print(cleanedstring)
        #entry = cleanedstring
        dfc1.append(cleanedstring)
        cleanedstring = ""

    return pd.Series(dfc1)

tfifgbdf_vect= TfidfVectorizer()
s2 = pd.Series(['the cat', 'awesome xyz', 'f_g_h lol asd'])
tfifgbdf_vect.fit_transform(s2)
s3 = pd.Series(['the dog the awesome xyz', 'xyz lol asd', 'f_g_h lol aha'])
clean_unseen(s3, tfifgbdf_vect)
...