Attributeerror ‘list’ object has no attribute ‘head’ pandas ( Solved )

Attributeerror 'list' object has no attribute 'head' pandas ( Solved )

A list is a data structure that allows you to store multiple variables in a single variable. There are some functions in list that are not present in it. For example, if you use the head() function on the list then you will get the attributeerror: ‘list’ object has no attribute ‘head’ pandas error. In this entire post, you will learn how to solve it easily.

Why attributeerror ‘list’ object has no attribute ‘head’ pandas error occurs

The main reason or root cause of getting this error is that you are using the functions on the list that are not available. The head() function allows you to display the top five rows of the pandas dataframe. You will know all the functions on the list when you will use the dir(your_list).

You will clearly see there is no head() function for the list.

dir(sample_list)

Output

['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']

You will get the attributeerror when you will run the below lines of code.

sample_list = [10,20,30,40,50,60,70,80,90,100]
sample_list.head()

Output

 

list object has no attribute head error
list object has no attribute head error

 

The solution of ‘list’ object has no attribute ‘head’ pandas error

The solution for this error is very simple. The head() function is for the panda dataframe. And if you want to use it then first convert the list to a pandas dataframe and then use that function.

To convert the list to pandas dataframe you will pass your sample list as an argument to the constructor pd.DataFrame().

Add the below lines of code to convert.

df = pd.DataFrame(sample_list)

At last, you can use the head() function in the converted list. You will not get the attributeerror ‘list’ object has no attribute ‘head’ pandas error.

import pandas as pd
sample_list = [10,20,30,40,50,60,70,80,90,100]
df = pd.DataFrame(sample_list)
df.head()
Output
top 5 rows for dataframe
top 5 rows for dataframe

Conclusion

You may get attributeerror on any function as that function may not be available in that module or object. If you are getting the ‘list’ object has no attribute ‘head’ pandas then it’s obvious that head() function is not available in the list. It is a function of the pandas module. The above method will solve this error.

I hope you have liked this tutorial. If you have any 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.

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