【问题标题】:Pandas - AttributeError: 'DataFrame' object has no attribute 'map'Pandas - AttributeError:“DataFrame”对象没有属性“map”
【发布时间】:2019-07-03 14:45:52
【问题描述】:

我正在尝试通过基于现有列创建字典并在该列上调用“地图”函数来在数据框中创建一个新列。它似乎工作了一段时间。然而,笔记本开始扔了

AttributeError: 'DataFrame' 对象没有属性 'map'

我没有更改内核或 python 版本。这是我正在使用的代码。

dict= {1:A,
       2:B,
       3:C,
       4:D,
       5:E}

# Creating an interval-type 
data['new'] = data['old'].map(dict)

如何解决这个问题?

【问题讨论】:

标签: python-3.x pandas


【解决方案1】:
import pandas as pd
f_dict = {1:0,2:1,3:2}
m = pd.Series([1,2,3])
res = m.map(f_dict)
print(res)

没关系,因为 m 是 pd.Series 对象。以下用法是错误的,因为 m 是 pd.DataFrame 对象。

import pandas as pd
f_dict = {1:0,2:1,3:2}
m = pd.DataFrame([1,2,3])
res = m.map(f_dict)
print(res)

【讨论】:

    【解决方案2】:

    主要问题是在选择old 列后得到DataFrame 而不是Series,所以map 实现尚未Series 失败。

    这里应该重复列old,所以如果选择一列,它将返回DataFrame中的所有列old

    df = pd.DataFrame([[1,3,8],[4,5,3]], columns=['old','old','col'])
    print (df)
       old  old  col
    0    1    3    8
    1    4    5    3
    
    print(df['old'])
       old  old
    0    1    3
    1    4    5
    
    #dont use dict like variable, because python reserved word
    df['new'] = df['old'].map(d)
    print (df)
    

    AttributeError: 'DataFrame' 对象没有属性 'map'

    重复删除此列的可能解决方案:

    s = df.columns.to_series()
    new = s.groupby(s).cumcount().astype(str).radd('_').replace('_0','')
    df.columns += new
    print (df)
       old  old_1  col
    0    1      3    8
    1    4      5    3
    

    另一个问题应该是MultiIndex列,测试:

    mux = pd.MultiIndex.from_arrays([['old','old','col'],['a','b','c']])
    df = pd.DataFrame([[1,3,8],[4,5,3]], columns=mux)
    print (df)
      old    col
        a  b   c
    0   1  3   8
    1   4  5   3
    
    print (df.columns)
    MultiIndex(levels=[['col', 'old'], ['a', 'b', 'c']],
               codes=[[1, 1, 0], [0, 1, 2]])
    

    并且解决方案是扁平化MultiIndex

    #python 3.6+
    df.columns = [f'{a}_{b}' for a, b in df.columns]
    #puthon bellow
    #df.columns = ['{}_{}'.format(a,b) for a, b in df.columns]
    print (df)
       old_a  old_b  col_c
    0      1      3      8
    1      4      5      3
    

    另一种解决方案是使用元组映射MultiIndex 并分配给新的tuple

    df[('new', 'd')] = df[('old', 'a')].map(d)
    print (df)
      old    col new
        a  b   c   d
    0   1  3   8   A
    1   4  5   3   D
    
    print (df.columns)
    MultiIndex(levels=[['col', 'old', 'new'], ['a', 'b', 'c', 'd']],
               codes=[[1, 1, 0, 2], [0, 1, 2, 3]])
    

    【讨论】:

    • 感谢您的详细解答!问题确实是因为重复的列。我永远不会怀疑这一点。所以我打电话 pd.concat 在 4 个不同的列上调用 get_dummies。我在 4 个不同的 pd.concat 调用中这样做。但是,我将其更改为单个 pd.concat 调用,并将数据框和列属性传递给 get_dummies。出于某种原因,此更改开始创建重复的列,我不得不恢复此更改。
    【解决方案3】:

    map 是一种可以在 pandas.Series 对象上调用的方法。 pandas.DataFrame 对象上不存在此方法。

    df['new'] = df['old'].map(d)
    

    在您的代码中 ^^^ df['old'] 出于某种原因正在返回 pandas.Dataframe 对象。

    • 正如@jezrael 指出的那样,这可能是由于数据框中有多个 old 列。
    • 或者您的代码可能与您给出的示例不太一样。

    • 无论哪种方式都会出现错误,因为您在 pandas.Dataframe 对象上调用 ma​​p()

    【讨论】:

    • @jezrael 如果他的代码不同,为什么会是 keyerror?
    猜你喜欢
    • 2017-01-24
    • 2017-02-12
    • 2014-01-10
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2018-10-10
    相关资源
    最近更新 更多