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' object has no attribute 'map'
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)
how to fix this?
Main problem is after selecting
old
column getDataFrame
insteadSeries
, somap
implemented yet toSeries
failed.Here should be duplicated column
old
, so if select one column it return all columnsold
inDataFrame
:Possible solution for deduplicated this columns:
Another problem should be
MultiIndex
in column, test it by:And solution is flatten
MultiIndex
:Another solution is map by
MultiIndex
with tuple and assign to newtuple
:map is a method that you can call on a pandas.Series object. This method doesn't exist on pandas.DataFrame objects.
In your code ^^^ df['old'] is returning a pandas.Dataframe object for some reason.
Or perhaps your code isn't quite the same as the example you have given.
Either way the error is there because you are calling map() on a pandas.Dataframe object