AttributeError: dataframe object has no attribute tolist ( Solved )

AttributeError_ dataframe object has no attribute tolist ( Solved )

Pandas is the best python package for dataframe creation and manipulation. Let’s say you want to convert some specific column values to a list. Then you may encounter the error Attributeerror: dataframe object has no attribute tolist. Many beginners try to solve this error but they are unable to do so. In this post, you will know how to solve the dataframe object has no attribute tolist errors in a very easy way.

Why error dataframe object has no attribute tolist Occurs?

The main or root cause for this error is that you are not using the tolist() function in a proper way. The tolist() function allows you to convert pandas dataframe column or series to a list. Most of the coders don’t use this function in a proper way.

For example when you will run the below lines of code then you will get the dataframe object has no attribute tolist error.

import pandas as pd
import numpy as np
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
        "country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
df.tolist()

Output

AttributeError 'DataFrame' object has no attribute 'tolist'
AttributeError ‘DataFrame’ object has no attribute ‘tolist’

The solution of dataframe object has no attribute tolist

The solution for this attribute error is that you should not apply the tolist() function in the entire column. Instead, use the function on a specific column or series. It is due to the fact that tolist() creates a single-dimensional array not a multi-dimensional array or data structure. For example, if I want to convert the name column to a series then you will use the following lines of code.

import pandas as pd
import numpy as np
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
        "country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
df["name"].tolist()

Output

Changing the name column values to list
Changing the name column values to list

You can see now the error is not coming.

Conclusion

AttributeError mostly comes when you are not properly using the function. In this tutorial, you have learned how to use the tolist() function to solve the error AttributeError : dataframe object has no attribute tolist error. Instead of using the tolist() function on the entire dataframe use it on a specific column.

I hope you have liked this tutorial. If you have any suggestions or queries then you can 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.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner