Dataframe constructor not properly called error ( Solved)

Dataframe constructor not properly called error

Pandas module allows you to create and manipulate dataframe. You can create a pandas dataframe using the pandas.DataFrame() constructor. But many beginners may face errors like ValueError: DataFrame constructor not properly called! And it can be due to many factors. In this entire post, you will learn how to solve the issue of dataframe constructor not properly called error.

Why the dataframe constructor not properly called error Occurs

The main reason for getting this error is that you must be using the DataFrame() constructor in the wrong way. For example, you must be passing the data to the constructor of an invalid type.

Suppose I will pass the data of type string then I will get the constructor not properly called error.

import pandas as pd
data = "Data Science Learner" #string value
df = pd.DataFrame(data)
print(df)

Output

passing the data of type string
passing the data of type string

In the same way, If I will pass the integer or float will get the same error.

import pandas as pd
data = 1000 # integer value
df = pd.DataFrame(data)
print(df)

Output

passing the data of type integer
passing the data of type integer
import pandas as pd
data = 10.11 # float value
df = pd.DataFrame(data)
print(df)

Output

passing the data of type float
passing the data of type float

Solution for the dataframe constructor not properly called error

The solution for this type of error is that you have to pass the data to constructor pandas.DataFrame() in a  proper way. For example you will pass the data to the constructor in the form key-value pairs like below. The number of elements for each of the column name should be the same otherwise it will lead to an error.

import pandas as pd
data = {"col1":[1,2,3],"col2":[4,5,6]}
df = pd.DataFrame(data)
print(df)

Output

passing the data as a dictionary
passing the data as a dictionary

The other solution is to add the data to the pandas.DataFrame() as the list of lists. In this case, each list will act as a record or row of the dataframe.

You will not get the error when you will execute the below lines of code.

import pandas as pd
data = [["Robin",10],["Maya",20],["George",30]]
df = pd.DataFrame(data)
print(df)

Output

passing the data as a list of list
passing the data as a list of list

Conclusion

If you are getting the error ValueError constructor not properly called! then the obvious case for getting is that you are not properly using the pandas.DataFrame() constructor. The above way of creating dataframe will solve this error.

I hope you have liked this tutorial and solved your query. In case you have any doubts 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