ValueError: DataFrame constructor not properly called! - Pandas

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.