python AttributeError:“str”对象没有属于panda数据fram的对象的属性“”

2024-04-20 07:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将我创建的dataframe对象中包含我需要的值的一部分分配给一个新对象。但是,当我试图运行python文件时,我得到了一个错误,该文件指出string对象没有我创建的包含值的对象的属性。不知道怎么了。在

AttributeError: 'str' object has no attribute 'vowel_map'

在培训.txt文件是:

^{pr2}$

我运行的python文件是:

import pandas as pd
import string

vowels = ('AA','AE','AH','AO','AW','AY','EH','ER','EY','IH','IY','OW','OY','UH','UW')

def remove_stress(string):
    if type(string) in [list, tuple]:
        string = ' '.join(string)
    return ''.join([i for i in string if not i.isdigit()]).split()

def phoneme_map(phon_list, phoneme_list):
    return [1 if phoneme in phoneme_list else 0 for phoneme in phon_list]

def get_words(file_path):
    words = pd.read_csv(file_path, sep=':', names = ['word', 'string_of_phon'])
    words['phon_list'] = words.string_of_phon.apply(str.split)
    words['stressless_phon_list'] = words.string_of_phon.apply(remove_stress)
    words['vowel_map'] = words.stressless_phon_list.apply(phoneme_map, args = (vowels,))

    return words

if __name__ == '__main__':
    data_loc = 'training.txt'
    words = get_words(data_loc)

    word_vowels = [word.vowel_map for word in words]

Tags: 文件对象inmapstringreturnifdef
2条回答

如果你想要一个热编码元音:

from sklearn.feature_extraction.text import CountVectorizer

vowels = ['AA','AE','AH','AO','AW','AY','EH','ER','EY','IH','IY','OW','OY','UH','UW']

df = pd.read_csv(file_path, sep=':', names = ['word', 'string_of_phon'])
vect = CountVectorizer(vocabulary=[v.lower() for v in vowels])    
X = vect.fit_transform(df['string_of_phon'].str.replace(r'\d+', ''))    
r = pd.DataFrame(X.A, columns=vect.get_feature_names(), index=df.index)

收益率

^{pr2}$

您可以将其与原始DF连接:

In [139]: df.join(r)
Out[139]:
            word               string_of_phon  ao  er  uw  eh  oy  ey  ow  ih  uh  ah  ay  iy  ae  aw  aa
0           COED                  K OW1 EH2 D   0   0   0   1   0   0   1   0   0   0   0   0   0   0   0
1        PURVIEW                P ER1 V Y UW2   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0
2          HEHIR              HH EH1 HH IH0 R   0   0   0   1   0   0   0   1   0   0   0   0   0   0   0
3       MUSCLING         M AH1 S AH0 L IH0 NG   0   0   0   0   0   0   0   1   0   2   0   0   0   0   0
4   NONPOISONOUS  N AA0 N P OY1 Z AH0 N AH0 S   0   0   0   0   1   0   0   0   0   2   0   0   0   0   1
5      LAVECCHIA        L AA0 V EH1 K IY0 AH0   0   0   0   1   0   0   0   0   0   1   0   1   0   0   1
6        BUCKLED              B AH1 K AH0 L D   0   0   0   0   0   0   0   0   0   2   0   0   0   0   0
7          EATEN                  IY1 T AH0 N   0   0   0   0   0   0   0   0   0   1   0   1   0   0   0
8         SCIMED                S AY1 M EH2 D   0   0   0   1   0   0   0   0   0   0   1   0   0   0   0
9         MORTIS              M AO1 R T IH0 S   1   0   0   0   0   0   0   1   0   0   0   0   0   0   0
10   CONSERVATOR    K AH0 N S ER1 V AH0 T ER0   0   2   0   0   0   0   0   0   0   2   0   0   0   0   0

IIUC:

In [85]: vowels = set(vowels)

In [86]: words['vowel_map'] =  \
            words['string_of_phon'].str.replace(r'\d+', '').str.split() \
                 .apply(lambda x: [int(i in vowels) for i in x])

In [87]: words
Out[87]:
            word               string_of_phon                       vowel_map
0           COED                  K OW1 EH2 D                    [0, 1, 1, 0]
1        PURVIEW                P ER1 V Y UW2                 [0, 1, 0, 0, 1]
2          HEHIR              HH EH1 HH IH0 R                 [0, 1, 0, 1, 0]
3       MUSCLING         M AH1 S AH0 L IH0 NG           [0, 1, 0, 1, 0, 1, 0]
4   NONPOISONOUS  N AA0 N P OY1 Z AH0 N AH0 S  [0, 1, 0, 0, 1, 0, 1, 0, 1, 0]
5      LAVECCHIA        L AA0 V EH1 K IY0 AH0           [0, 1, 0, 1, 0, 1, 1]
6        BUCKLED              B AH1 K AH0 L D              [0, 1, 0, 1, 0, 0]
7          EATEN                  IY1 T AH0 N                    [1, 0, 1, 0]
8         SCIMED                S AY1 M EH2 D                 [0, 1, 0, 1, 0]
9         MORTIS              M AO1 R T IH0 S              [0, 1, 0, 0, 1, 0]
10   CONSERVATOR    K AH0 N S ER1 V AH0 T ER0     [0, 1, 0, 0, 1, 0, 1, 0, 1]

现在可以将计算列指定给另一个对象:

^{pr2}$

相关问题 更多 >