‘dataframe’ object has no attribute ‘map’ ( Solved )

'dataframe' object has no attribute 'map'

Pandas is a python module that allows you to create a dataframe and manipulate the dataset columns and rows. There are many functions in it to achieve this. You can also get errors like AttributeError. You will get the ‘dataframe’ object has no attribute ‘map’ error when you will apply the map function to the columns.

In this entire tutorial, you will know why this ‘dataframe’ object has no attribute ‘map’ error comes and how to solve it.

What is Map Function?

The map function in python allows you to process and transform all the iterable items without the use of any loop. The pandas also provide map() function that you can apply to the series. First, you have to define some task or function you want to perform and then applying to all the rows using it.

Why AttributeError : ‘dataframe’ object has no attribute ‘map’ Error occur?

You are getting the error ‘dataframe’ object has no attribute ‘map’ because the map() function cannot be applied to the dataframes. Let’s create a sample dataframe to demonstrate this example.

import pandas as pd
data = {"col1":[10,20,30],"col2":[40,50,60]}
df = pd.DataFrame(data)
print(df)

Output

Sample dataframe for map error
Sample dataframe for map error

Create a function that will use in the map() function as the argument. It will multiply all the values of the dataframe with 2.

def multiply(x):
  return x*2

Here the function will accept the value of the row and return after multiplying by 2.

Now if you will apply the multiply() function to the dataframe using the map() function then you will get the no attribute ‘map’ error.

df.map(multiply)

Output

Dataframe object has not attribute map error
Dataframe has not attribute map error

How to solve ‘dataframe’ object has no attribute ‘map’ Error

You can solve this error using the two ways.

Solution 1: Use map() function on series

If you want to execute the map() function on the dataframe then you can only do it in series, not on the Dataframes. Let’s run the same function on col2.

Run the below lines of code.

df["col2"].map(multiply)
Output
applying the map function on the series
applying the map function on the series

You can see you are not getting the error.

Solution 2:  Use the apply() function

There is only apply() function in pandas that allows you to execute the function on all the columns. You have to just pass the function inside the apply() as an argument.
import pandas as pd
data = {"col1":[10,20,30],"col2":[40,50,60]}
df = pd.DataFrame(data)
def multiply(x):
  return x*2
result_df=df.apply(multiply,axis=1)
print(result_df)

Output

Applying the apply() function on the dataframe
Applying the apply() function on the dataframe

Again you are not getting the no attribute ‘map’ error when you run the code.

Conclusion

You are getting the AttributeError ‘dataframe’ object has no attribute ‘map’ as you are applying the function using the map() function. The map() functions work only on series, not the dataframe. If you want to manipulate or change the values to all the columns at once then use the dataframe.apply() method.

I hope you have liked this tutorial. If you are still getting the error then contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner