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

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

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)
You can see you are not getting the error.
Solution 2: Use the apply() function
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

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.