【问题标题】:AttritubeError: list' object has no attribute 'map' when using .apply() to dataFrameAttritubeError:使用 .apply() 到 dataFrame 时,list' 对象没有属性 'map'
【发布时间】: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 的函数之外以非常手动和时间顺序的方式使用。我无法弄清楚似乎出了什么问题。

【问题讨论】:

标签: python list dataframe


【解决方案1】:

您会得到该结果,因为当您将 apply() extractGrammar() 函数传递给 DataFrame 时,它​​会将 DataFrame 的每一行传递给函数。然后,当您访问 ['POS Tag'] 列时,它不会返回整个系列,而是返回该行的 POS Tag 单元格的内容,这是一个列表。列表没有map 方法。如果您尝试计算 POS Tag 列中每个元组的第二个元素的出现次数,您可以尝试以下操作:

tag_count_data = Counter([x[1] for x in email['POS Tag']])

这将为您提供该单独行的标签的第二个元素的计数器。

【讨论】:

    【解决方案2】:

    为了使用我在问题上发布的标签并基于LiamFiddler 的善意指导的 df,我后来继续:

    1. 使用 dict() 将 Counter 对象转换为 dict
    2. 我把 dict 变成了 Series,
    3. 我将列值设置为列名based on this answer
    4. 然后继续选择我的 dataDrame 所需的标签。
    def extractGrammar(email): 
       # Updated calculate the tags I need 
       tag_count_data = Counter([x[1] for x in email['POS_Tag']])
      
       #Convert the Counter object to dict
       tag_count_dict = dict(tag_count_data)
    
       #Turning dict into Series
       email_tag = pd.DataFrame(pd.Series(tag_count_dict).fillna(0).rename_axis('Tag'))
       email_tag = email_tag.reset_index()
    
       #use set_index to set Tag column values to be column names
       email_tag= email_tag.set_index("Tag").T.reset_index(drop=True).rename_axis(None, axis=1) 
       
       #select Tags that I need
       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_tag.columns:
           email_tag[pos] = 0
    
       email_tag = email_tag[pos_columns] 
    
       return email_tag
    
    

    【讨论】:

      猜你喜欢
      • 2017-01-24
      • 2019-01-15
      • 1970-01-01
      • 2019-07-03
      • 2017-01-15
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多