pandas -AttributeError:"DataFrame"对象没有属性"map" [英] Pandas - AttributeError: 'DataFrame' object has no attribute 'map'

查看:124
本文介绍了 pandas -AttributeError:"DataFrame"对象没有属性"map"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在现有列的基础上创建字典并在列上调用地图"功能来在数据框中创建新列.它似乎已经工作了相当长的时间.但是,笔记本电脑开始抛出

I am trying to create a new column in an dataframe, by creating a dictionary based on an existing column and calling the 'map' function on the column. It seemed to be working for quite some time. However, the notebook started throwing

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

AttributeError: 'DataFrame' object has no attribute 'map'

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

I haven't changed the kernel or the python version. Here's the code i am using.

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

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

该如何解决?

推荐答案

主要问题是在选择old列后获取了DataFrame而不是Series,所以map实施到Series失败了.

Main problem is after selecting old column get DataFrame instead Series, so map implemented yet to Series failed.

此处应重复列old,因此,如果选择一个列,则会返回DataFrame中的所有列old:

Here should be duplicated column old, so if select one column it return all columns old in DataFrame:

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"

AttributeError: 'DataFrame' object has no attribute 'map'

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

Possible solution for deduplicated this columns:

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,通过以下方法进行测试:

Another problem should be MultiIndex in column, test it by:

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:

Another solution is map by MultiIndex with tuple and assign to new 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]])

这篇关于 pandas -AttributeError:"DataFrame"对象没有属性"map"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆