I want to assign a part of a dataframe object I created that contain values that I need to a new object. However, I'm getting an error when I try to run my python file, which says that the string object has no attribute for the object that I created containing the values. Not sure what is wrong.
AttributeError: 'str' object has no attribute 'vowel_map'
The training.txt file is:
COED:K OW1 EH2 D
PURVIEW:P ER1 V Y UW2
HEHIR:HH EH1 HH IH0 R
MUSCLING:M AH1 S AH0 L IH0 NG
NONPOISONOUS:N AA0 N P OY1 Z AH0 N AH0 S
LAVECCHIA:L AA0 V EH1 K IY0 AH0
BUCKLED:B AH1 K AH0 L D
EATEN:IY1 T AH0 N
SCIMED:S AY1 M EH2 D
MORTIS:M AO1 R T IH0 S
CONSERVATOR:K AH0 N S ER1 V AH0 T ER0
The python file I'm running is:
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]
word_vowels = words.vowel_map? It's hard to understand not being able to see your desired data set...wordsis a DataFrame object, whose__iter__yields the column names (strings). I'm guessing you actually want to iterate through the rows, selecting only the'word_vowel'values. If that's what you want, then @MaxU suggestion is the way to go. If in the future you would like to iterate through the rows of a dataframe, you could usedf.iterrows().