Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - AttributeError: 'DataFrame' object has no attribute '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' 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?

like image 528
redwolf_cr7 Avatar asked Feb 09 '19 16:02

redwolf_cr7


People also ask

How do you convert PySpark DF to pandas DF?

Convert PySpark Dataframe to Pandas DataFramePySpark DataFrame provides a method toPandas() to convert it to Python Pandas DataFrame. toPandas() results in the collection of all records in the PySpark DataFrame to the driver program and should be done only on a small subset of the data.

How do you convert a data frame to a series?

Convert DataFrame Row to SeriesUse squeeze() function to convert the single Pandas DataFrame row to series. For instance, df. iloc[2]. reset_index(drop=True).

How do you convert a DataFrame to a list?

At times, you may need to convert your pandas dataframe to List. To accomplish this task, ' tolist() ' function can be used.


1 Answers

map is a method that you can call on a pandas.Series object. This method doesn't exist on pandas.DataFrame objects.

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

In your code ^^^ df['old'] is returning a pandas.Dataframe object for some reason.

  • As @jezrael points out this could be due to having more than one old column in the dataframe.
  • 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

like image 162
Arran Duff Avatar answered Sep 25 '22 17:09

Arran Duff