【发布时间】:2021-10-17 23:26:21
【问题描述】:
这就是我的名为“电子邮件”的数据框的样子(只有一行包含“文本”和“POS_Tag”列):
打印(电子邮件)
我试图在我的数据帧上使用apply(),首先将函数定义为:
def extractGrammar(email):
tag_count_data = pd.DataFrame(email['POS_Tag'].map(lambda x: Counter(tag[1] for tag in x)).to_list())
# Print count Part of speech tag needed for Adjective, Adverbs, Nouns and Verbs
email = pd.concat([email, tag_count_data], axis=1).fillna(0)
pos_columns = ['PRP','MD','JJ','JJR','JJS','RB','RBR','RBS', 'NN', 'NNS','VB', 'VBS', 'VBG','VBN','VBP','VBZ']
for pos in pos_columns:
if pos not in email.columns:
email[pos] = 0
email = email[['text'] + pos_columns]
email['Adjectives'] = email['JJ'] + email['JJR'] + email['JJS']
email['Adverbs'] = email['RB'] + email['RBR'] + email['RBS']
email['Nouns'] = email['NN'] + email['NNS']
email['Verbs'] = email['VB'] + email['VBS'] + email['VBG'] + email['VBN'] + email['VBP'] + email['VBZ']
return email
我尝试使用 apply() 函数将我的电子邮件作为对象传递:
emails = emails.apply(extractGrammar, axis=1)
我刚刚收到此错误:
AttributeError: 'list' object has no attribute 'map'
我之前在包含多行电子邮件的 CSV 文件的“extractGrammar”函数中使用了完全相同的代码块,但它在未使用 apply 的函数之外以非常手动和时间顺序的方式使用。我无法弄清楚似乎出了什么问题。
【问题讨论】: