Dataframe constructor not properly called ошибка

I am new to Python and I am facing problem in creating the Dataframe in the format of key and value i.e.

data = [{'key':'[GlobalProgramSizeInThousands]','value':'1000'},]

Here is my code:

columnsss = ['key','value'];
query = "select * from bparst_tags where tag_type = 1 ";
result = database.cursor(db.cursors.DictCursor);
result.execute(query);
result_set = result.fetchall();
data = "[";
for row in result_set:
`row["tag_expression"]`)
    data +=  "{'value': %s , 'key': %s }," % ( `row["tag_expression"]`, `row["tag_name"]` )
data += "]" ;    
df = DataFrame(data , columns=columnsss); 

But when I pass the data in DataFrame it shows me

pandas.core.common.PandasError: DataFrame constructor not properly called!

while if I print the data and assign the same value to data variable then it works.

Syscall's user avatar

Syscall

19.2k10 gold badges36 silver badges52 bronze badges

asked Sep 1, 2014 at 10:47

Ravi khatri's user avatar

1

You are providing a string representation of a dict to the DataFrame constructor, and not a dict itself. So this is the reason you get that error.

So if you want to use your code, you could do:

df = DataFrame(eval(data))

But better would be to not create the string in the first place, but directly putting it in a dict. Something roughly like:

data = []
for row in result_set:
    data.append({'value': row["tag_expression"], 'key': row["tag_name"]})

But probably even this is not needed, as depending on what is exactly in your result_set you could probably:

  • provide this directly to a DataFrame: DataFrame(result_set)
  • or use the pandas read_sql_query function to do this for you (see docs on this)

answered Sep 1, 2014 at 11:24

joris's user avatar

jorisjoris

132k36 gold badges246 silver badges202 bronze badges

1

Just ran into the same error, but the above answer could not help me.

My code worked fine on my computer which was like this:

test_dict = {'x': '123', 'y': '456', 'z': '456'}
df=pd.DataFrame(test_dict.items(),columns=['col1','col2'])

However, it did not work on another platform. It gave me the same error as mentioned in the original question. I tried below code by simply adding the list() around the dictionary items, and it worked smoothly after:

df=pd.DataFrame(list(test_dict.items()),columns=['col1','col2'])

Hopefully, this answer can help whoever ran into a similar situation like me.

answered Mar 29, 2021 at 17:00

dayaoyao's user avatar

dayaoyaodayaoyao

1102 silver badges9 bronze badges

import json

# Opening JSON file
f = open('data.json')

# returns JSON object as
# a dictionary
data1 = json.load(f)

#converting it into dataframe
df = pd.read_json(data1, orient ='index')

answered Aug 1, 2022 at 4:56

Amit vijay singh's user avatar

1

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.

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.

We respect your privacy and take protecting it seriously

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

Something went wrong.

How to fix valueerror dataframe constructor not properly calledThe error code that reads valueerror: dataframe constructor not properly called! occurs when using a data analysis tool like Pandas or Pyspark. It can be confusing to figure out the cause of the error but don’t fret. Keep on reading as our experts teach you what causes this error and how you can fix it. Our explanation will be in-depth, so you are in good hands.

Contents

  • Why Is the Dataframe Constructor Not Properly Called?
    • – You Provided a String Representation to the Dataframe Constructor
    • – You Misused the Input Types to Pandas Dataframe
    • – You Used the Wrong Parameter to Pandas Dataframe
    • – There Is a Mismatch Between Python and Azure-ML Libraries
  • How To Fix Dataframe Constructor Not Called
    • – Use a Dictionary for Pandas.dataframe
    • – Provide the Right Input to the Dataframe
    • – Use the Right Parameter for the DataFrame
    • – Switch Python Version in Azure
  • Useful Information About the Dataframe Error
    • – What Is a Value Error?
    • – How To Convert Json to a Dataframe?
    • – How To Convert a List to a Dataframe?
    • – How To Make Research About Python and Dataframe?
    • – How Do You Create a Dataframe in Python?
    • – How To Create a Dataframe From Another Dataframe in Pandas?
  • Conclusion

The DataFrame Constructor is not called properly because:  you provided a string representation to the pandas.DataFrame Constructor, you misused the input types to Pandas Dataframe, you used the wrong parameter to Pandas DataFrame, or there is a mismatch between Python and azure-ml libraries.

Let’s take a closer look at these possible reasons.

– You Provided a String Representation to the Dataframe Constructor

The DataFrame constructor requires that its input be an iterable, a dictionary, or another DataFrame. Failure to adhere to any of the requirements will lead to a ValueError. That’s because the string that you’ve provided can not work with pandas.DataFrame.

In the sample code below, we’ve violated the requirements for the pandas.DataFrame, so the code will result in a ValueError.

myData = [“-35054”, “2018-09-15T09:09:23.340Z”, “2018-09-15T09:09:23.340Z”]

dataframe_test = DataFrame(index = idx, data=(myData))

– You Misused the Input Types to Pandas Dataframe

Pandas clearly define the input types that you must use, but it’s easy to think that it’ll do otherwise. Which raises the question: What if I supply a number instead of what Pandas require? Well, things won’t go as planned, and you’ll get the ValueError that the Constructor was not called properly.

For example, in the code below, we’ve supplied a number to Pandas. Therefore, any usage of the code will lead to a ValueError.

import pandas as p_d
p_d.DataFrame(5000)

Furthermore, in the example below, we used a string in the DataFrame, so we get the same ValueError.

data_frame = p_d.DataFrame(‘Email’)

– You Used the Wrong Parameter to Pandas Dataframe

When you are working with images in Pandas, you can see the ValueError about the Constructor. Meanwhile, if your code is a lot, it can be tough to know how to fix the error. We present an example in the next code.

In the code below, we’ve used the result of a calculation as the value of pandas.DataFrame. As a result, the code will result in an error.

def detect_Image(self):
img = self.aux4
(_, contours, _) = cv.findContours(img, cv.RETR_EXTERNAL,
cv.CHAIN_APPROX_SIMPLE)
c = [cv.minEnclosingCircle(cnt) for cnt in contours]
print (‘Circles: ‘, len(c), ‘Type: ‘,
type(c))
for i in range(len(c)):
r = ci
a = pi * (r * r)
a = a * self.Scal
self.res = pd.dataframe(a)
print self.res

– There Is a Mismatch Between Python and Azure-ML Libraries

You can run into an error with pandas in a Python script when doing machine learning with Microsoft Azure. This error will happen if you are running Python 2.7.7 (sklearn v.0.15.1). As a result, it’ll return a non-zero exit code.

How To Fix Dataframe Constructor Not Called

You can fix the pandas.DataFrame not called by using a Dictionary for pandas.Dataframe, providing the right input to the DataFrame, using the right parameter for the DataFrame, or switching Python version in Azure.

In this section, we’ll be going more in-depth regarding the steps you can take to solve this error.

– Use a Dictionary for Pandas.dataframe

Using a dictionary for pandas.DataFrame will prevent the Constructor not called error. In Python, you use a convert string to dataframe Python code. This will allow you to use a dataframe in pandas.DataFrame. Another option is to create dataframe to use with Python Pandas.

In the example below, we’ve made corrections to the code. The corrections will prevent the ValueError.

import ast
myData = [“-35054”, “2018-09-15T09:09:23.340Z”, “2018-09-15T09:09:23.340Z”]
# convert string to a dict
dict = ast.literal_eval(myData)
# Use as input to the dataframe
df_test2 = DataFrame(index = idx, data=(dict))

– Provide the Right Input to the Dataframe

The right input to DataFrame will prevent the ValueError about the Constructor. In the code below, we modified a previous example. Only this time, the DataFrame has the correct input.

data_frame = p_d.DataFrame(columns=[‘Email’])

In PySpark, the right input will prevent valueerror: dataframe constructor not properly called! pyspark error. Also, this applies in Plotyl express, as it eliminates the valueerror dataframe constructor not properly called plotly express error. What’s more, in Scikit, you’ll get the following error code if you are using the wrong method to create a DataFrame:

in init raise valueerror dataframe constructor not properly called

– Use the Right Parameter for the DataFrame

Using the right parameter for the DataFrame will eliminate the error that will occur with DataFrame. Earlier in this article, we showed you an example of a code that does some image processing. However, the code resulted in an error stating that the Constructor was not called properly.

In the code below, we’ve made corrections that will prevent the error.

def detect_Image(self):
img = self.aux4
(_, contours, _) = cv.findContours(img, cv.RETR_EXTERNAL,
cv.CHAIN_APPROX_SIMPLE)
c = [cv.minEnclosingCircle(cnt) for cnt in contours]
print(f”Circles: {len(circles)}, Type: {type(circles)}”)
areas_gen = (pi * (r * r) * self.Scal for _, radius in circles)
self.res = pd.dataframe({“Area”: areas_generated})
print(self.res)

From the code above, we’ve used a Python dictionary to eliminate the error. However, in some situations, you might use a list to solve this error. So you need to be wary of the error code that reads valueerror: if using all scalar values, you must pass an index, as this error means that you must use an index when using scalar values.

The following is an example of what we are talking about:

a = 2
b = 3
data_frame = pd.DataFrame({‘X’: x, ‘Y’: y}, index=[0])

However, the following line will produce a DataFrame error:

data_frame = pd.DataFrame({‘X’:y,’Y’:y}

– Switch Python Version in Azure

You get the Pandas error when you switch the python version in Azure. You can switch the Python version in an Execute Python Script module. All you have to do is select the Python Script, and a drop-down menu will appear on the right-hand side of the page. Choose another Python version.

Useful Information About the Dataframe Error

In this section, we’ll discuss additional relevant information that will help you understand why this error occurred in the first place. We’ll make it an interactive section, so we’ll pose some commonly-asked questions and we’ll give you the answers as well.

– What Is a Value Error?

A ValueError is an exception that Python raises when you supply an invalid value to a function. However, the value is a valid function. This definition best explains the reason why the DataFrame throws an error when you provide it with a string, because it expected a dictionary.

– How To Convert Json to a Dataframe?

You can convert JSON to DataFrame by using the read_json() function available in Pandas. However, when you are working with a nested JSON, you can use the json_normalize() function.

– How To Convert a List to a Dataframe?

You can convert a list to DataFrame by using a list with index and column names, using zip() function, creating from the multidimensional list, using a multidimensional list with column name, or using a list in the dictionary.

– How To Make Research About Python and Dataframe?

You can make further research about Python and DataFrame by checking the web for questions tagged Python. An example of such a site is StackOverflow. The research we are talking about should be about how to prevent the ValueError in DataFrame. When you do this, you’ll be in a better position to know what to check when this error occurs.

– How Do You Create a Dataframe in Python?

You can create a DataFrame in Python by importing Pandas into your code, creating the data as lists, creating a DataFrame using Pandas.DataFrame(), and printing the results.

The code below is the implementation of these steps.

import pandas as pd
# assign data
data = {‘Name’: [‘Frank’, ‘Michael’, ‘Maddison’, ‘Tony’], ‘Age’: [34, 25, 29, 28]}
# Create a DataFrame
data_frame = pd.DataFrame(data)
# Print the data
print(data_frame)

– How To Create a Dataframe From Another Dataframe in Pandas?

The DataFrame.assign() method allows you to create a new DataFrame from another DataFrame. It does this by assigning new columns to a DataFrame, so it returns a new object that contains the original columns added to the new ones.

Conclusion

This article explained how to fix the DataFrame error when using Pandas and related libraries. What’s more, we also answered some frequent questions about the error as a whole. The following are the main points that we discussed in this guide:

  • A wrong input will cause a DataFrame ValueError.
  • In Azure, mismatch in Python versions can cause a DataFrame ValueError.
  • The Constructor in the DataFrame expects values like a dictionary or another DataFrame.
  • The DataFrame.assign() method will create a DataFrame from another DataFrame.

Valueerror dataframe constructor not properly calledAt this stage, you are all set to fix the DataFrame ValueError in Pandas and related Python libraries.

  • Author
  • Recent Posts

Position is Everything

Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team

Position is Everything

Pandas doesn’t accept every data type to create a DataFrame. Sometimes it will give you the error “DataFrame constructor not properly called!”. Read the examples below to understand why it happens.

You are getting this error because, as the message implies, there is something wrong with the way the DataFrame() function is called. And there are many of reasons for this.

Slow down for a moment and have a look at this constructor again:

pandas.DataFrame(data, index, columns, dtype, copy)

This function will create a DataFrame, the primary data structure of the pandas module, from the content of the data parameter. This is the only required parameter of the function.

Pandas allows you to create DataFrames from various data structures. They can be Python iterables, dicts, other Pandas DataFrames, or NumPy ndarrays. If the data parameter is a dict, it must contain list-like objects like arrays, dataclass, constant, or Pandas Series.

Most of the time, pandas.DataFrame() returns the error “DataFrame constructor not properly called!” when you don’t follow those rules properly.

Examples And Solutions

Example 1

Pandas uses DataFrames to store data in a two-dimensional structure, just like columns and rows in a table. When the source of data you provide can’t be used to construct such a structure, you will end up with errors.

For instance, don’t use regular strings or numbers with the DataFrame() constructor:

>>> pd.DataFrame("LearnShareIT")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/site-packages/pandas/core/frame.py", line 756, in __init__
    raise ValueError("DataFrame constructor not properly called!")
ValueError: DataFrame constructor not properly called!

If you need to save only a string or number element into a DataFrame, put it into a list or dict instead:

>>> pd.DataFrame(["LearnShareIT"])
              0
0  LearnShareIT
>>> pd.DataFrame({10})
    0
0  10

Example 2

Another example is when you read a JSON file with open():

with open('example.json', 'r') as file:
    filedata = json.load(file)
    df = pd.DataFrame(file_list)

The DataFrame() constructor might not be able to get the data it needs from the json.load() function and generate an errors.

Pandas has a built-in solution for reading JSON files. You should use that instead:

df = pd.read_json('example.json')

Example 3

While Spark draws inspiration from DataFrames of Pandas, you can use them directly to convert to DataFrame in PySpark:

from pyspark.sql import Row
df = spark.createDataFrame([
    Row(a = 'LearnShareIT', b = 'tutorials', c = 'learnshareit.com'),
    Row(a = 'Quora', b = 'Q&A', c = 'quora.com')
])
 
df.show()
df2 = pd.DataFrame(df)

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/site-packages/pandas/core/frame.py", line 756, in __init__
    raise ValueError("DataFrame constructor not properly called!")
ValueError: DataFrame constructor not properly called!

Spark has its own function for this purpose – toPandas():

>>> df2 = df.toPandas()
>>> print(df2)
              a          b                 c
0  LearnShareIT  tutorials  learnshareit.com
1         Quora        Q&A         quora.com

Remember that this function is only designed for small DataFrames, as Spark loads all of their elements to the drive’s memory.

Summary

Pandas will returns the error “DataFrame constructor not properly called!” when you don’t provide the correct data type to the DataFrame() constructor. Make sure it is an iterable, an ndarray, or a DataFrame.

Maybe you are interested in similar errors:

  • Series Objects Are Mutable and Cannot be Hashed
  • ConnectionError: Max retries exceeded with url
  • JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  • ValueError: invalid literal for int() with base 10 in Python
  • Defaulting to user installation because normal site-packages is not writeable

Robert J. Charles

My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.


Job: Developer
Name of the university: HUST
Major: IT
Programming Languages: Java, C#, C, Javascript, R, Typescript, ReactJs, Laravel, SQL, Python

Last updated on 
Oct 18, 2022

In this tutorial, we’ll take a look at the Pandas error:

ValueError: DataFrame constructor not properly called!

First, we’ll create examples of how to produce it.

Next, we’ll explain the reason and finally, we’ll see how to fix it.

ValueError: DataFrame constructor not properly called

Let’s try to create DataFrame by:

import pandas as pd
df = pd.DataFrame(0)
df = pd.DataFrame('a')

All of them result into error:

ValueError: DataFrame constructor not properly called!

The same will happen if we try to create DataFrame from directly:

class k:
      pass

a = k()
pd.DataFrame(a)

Reason

There are multiple reasons to get error like:

ValueError: DataFrame constructor not properly called!

Pass single value

One reason is trying to pass single value and no index:

df = pd.DataFrame('a')

this results in:

ValueError: DataFrame constructor not properly called!

Object to DataFrame

Sometimes we need to convert object as follows:

class k:
    def __init__(self, name, ):
        self.name = name
        self.num = 0

a = k('test')
pd.DataFrame(a)

This is not possible and result in:

ValueError: DataFrame constructor not properly called!

Solution — single value

To solve error — ValueError: DataFrame constructor not properly called — when we pass a single value we need to use a list — add square brackets around the value. This will convert the input vector value:

import pandas as pd
df = pd.DataFrame(['a'])

The result is successfully created DataFrame:

0
0 a

Solution — object to DataFrame

To convert object to DataFrame we can follow next steps:

class k:
    def __init__(self, name, ):
        self.name = name
        self.num = 0

a = k('test')

To convert object «a» to DataFrame first convert it to JSON data by:

import json

print(json.dumps(a.__dict__))

the result is:

'{"name": "test", "num": 0}'

Next we convert the JSON string to dict:

json.loads(data)

result:

{'name': 'test', 'num': 0}

And finally we create DataFrame by:

pd.DataFrame.from_dict(json.loads(data), orient='index').T

the object is converted to Pandas DataFrame:

name num
0 test 0

Conclusion

In this article, we discussed error: ValueError: DataFrame constructor not properly called! reasons and possible solutions.

Понравилась статья? Поделить с друзьями:
  • Data source reference is not valid ошибка
  • Data root ошибка
  • Dashboard сбой код ошибки 7
  • Dashboard huawei modem ошибка 10
  • Dash 3 media err decode ошибка