Pandas dataframes are used to manipulate tabular data in python. While data manipulation, we sometimes need to convert data from other python objects such as lists, strings, and tuples into a dataframe. During conversion, you might get into an exception with the message ValueError: DataFrame constructor not properly called! This article discusses the ValueError: DataFrame constructor not properly called! error, its causes, and the solutions.
- What Is ValueError: Dataframe Constructor Not Properly Called! Error in Python?
- When Does the ValueError: DataFrame Constructor Not Properly Called Error Occur?
- How to Solve ValueError: DataFrame Constructor Not Properly Called Exception in Python?
- Use the columns Parameter to Assign Column Names to the Dataframe
- Conclusion
What Is ValueError: Dataframe Constructor Not Properly Called! Error in Python?
As the message suggests the error “ValueError: DataFrame constructor not properly called! ” is a python ValueError exception. It means that the error occurs when we pass an incompatible value as input to the DataFrame()
function. This may happen in the following cases.
- We pass a string as the input to the
DataFrame()
function. - When we pass a string representation of a list instead of a list to the
DataFrame()
function. - We pass a JSON string directly to the
DataFrame()
function. - When we pass a string representation of a python dictionary instead of a dictionary to the
DataFrame()
function. - We pass other scalar values such as integers or floating point numbers to the
DataFrame()
function.
When Does the ValueError: DataFrame Constructor Not Properly Called Error Occur?
As introduced above the exception occurs in five cases. Let us discuss each of them one by one.
When We Pass a String as the Input to the DataFrame() Function
In most instances, ValueError: DataFrame constructor not properly called error occurs when we try to create an empty dataframe with a given column name. When we pass the column name directly to the DataFrame()
function, the program runs into the ValueError exception. You can observe this in the following example.
import pandas as pd
columnName="Column1"
print("The column name is:")
print(columnName)
df=pd.DataFrame(columnName)
print("The dataframe is:")
print(df)
Output:
The column name is:
Column1
ValueError: DataFrame constructor not properly called!
In this example, we tried to create an empty dataframe with the column name Column1
. As we passed the column name directly to the DataFrame()
function, the program runs into ValueError exception.
We Pass a String to the DataFrame() Function
We often create a dataframe from a list in python. You might think that you can also create a dataframe of characters in the string by passing the string to the DataFrame()
function as an input argument. However, the program runs into the ValueError: DataFrame constructor not properly called! Exception.
You can observe this in the following example.
import pandas as pd
myStr="PFB"
print("The string is:")
print(myStr)
df=pd.DataFrame(myStr)
print("The dataframe is:")
print(df)
Output:
The string is:
PFB
ValueError: DataFrame constructor not properly called!
In this example, we passed the string "PFB"
to the DataFrame()
function to create a dataframe. Due to this, the program runs into ValueError Exception.
When we pass a string representation of a list to the DataFrame()
function, the program runs into ValueError: DataFrame constructor not properly called exception as shown below.
import pandas as pd
listStr='[1,22,333,4444,55555]'
print("The list string is:")
print(listStr)
df=pd.DataFrame(listStr)
print("The dataframe is:")
print(df)
Output:
The list string is:
[1,22,333,4444,55555]
ValueError: DataFrame constructor not properly called!
In the above example, we passed a string "[1,22,333,4444,55555]"
to the DataFrame()
function. Due to this, the program runs into ValueError exception.
In a similar manner, when we pass a string representation of a dictionary to the DataFrame()
function, the program runs into a ValueError exception as shown in the following example.
import pandas as pd
dictStr='{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}'
print("The dictionary string is:")
print(dictStr)
df=pd.DataFrame(dictStr)
print("The dataframe is:")
print(df)
Output:
The dictionary string is:
{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}
ValueError: DataFrame constructor not properly called!
In some cases, we might also directly try to convert a JSON string into a pandas dataframe using the DataFrame()
function. In these cases, the program will run into errors as shown below.
import pandas as pd
jsonStr='{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}'
print("The json string is:")
print(jsonStr)
df=pd.DataFrame(jsonStr)
print("The dataframe is:")
print(df)
Output:
The json string is:
{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}
ValueError: DataFrame constructor not properly called!
When We Pass a Scalar Value to the DataFrame() Function
We can create a dataframe from an iterable object such as a list, tuple, set, or dictionary. However, when we pass an object of primitive data types such as integer or floating point number as input to the DataFrame()
function, the program runs into the ValueError exception with the message ValueError: DataFrame constructor not properly called!.
You can observe this in the following example.
import pandas as pd
myInt=1117
print("The integer is:")
print(myInt)
df=pd.DataFrame(myInt)
print("The dataframe is:")
print(df)
Output:
The integer is:
1117
ValueError: DataFrame constructor not properly called!
In this example, we passed the integer 1117 to the DataFrame()
function. Due to this, the program runs into ValueError exception.
How to Solve ValueError: DataFrame Constructor Not Properly Called Exception in Python?
In accordance with the reasons of the error, we can solve the ValueError: DataFrame constructor not properly called exception using various ways.
Use the columns Parameter to Assign Column Names to the Dataframe
The first way to solve the ValueError: DataFrame constructor not properly called exception in Python is to not pass a string directly to the DataFrame() constructor. If you are trying to create a dataframe with a given column name as a string, use the columns
parameter in the constructor as shown below.
import pandas as pd
columnName="Column1"
print("The column name is:")
print(columnName)
df=pd.DataFrame(columns=[columnName])
print("The dataframe is:")
print(df)
Output:
The column name is:
Column1
The dataframe is:
Empty DataFrame
Columns: [Column1]
Index: []
In this example, we passed the string "Column1"
to the columns
parameter after putting it in a list. Due to this, the program executes successfully and we get an empty dataframe with the given column name.
Pass a List of Strings as Input to the DataFrame() Function
If you want to create a dataframe from the characters of the list. You can first convert the string to a list of characters. Then, you can pass the list as input to the DataFrame()
constructor to avoid the ValueError: DataFrame constructor not properly called exception in Python. You can observe this in the following example.
import pandas as pd
myStr="PFB"
print("The string is:")
print(myStr)
df=pd.DataFrame(list(myStr))
print("The dataframe is:")
print(df)
Output:
The string is:
PFB
The dataframe is:
0
0 P
1 F
2 B
In this example, we first created a list of characters using the string "PFB"
and the list()
function. Then, we passed the list of characters to the the DataFrame()
function to create the output dataframe.
If you want to put the string as an element of the data frame, you can put the string in a list and then pass the list to the DataFrame
() function as shown below.
import pandas as pd
myStr="PFB"
print("The string is:")
print(myStr)
df=pd.DataFrame([myStr])
print("The dataframe is:")
print(df)
Output:
The string is:
PFB
The dataframe is:
0
0 PFB
Convert Strings Into Python Objects Before Passing Them to the DataFrame() Function
If you want to convert a JSON string to a dataframe, first convert the json string to a python dictionary. Then, you can pass the dictionary to the DataFrame()
function as shown below.
import pandas as pd
import json
jsonStr='{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}'
print("The json string is:")
print(jsonStr)
myDict=json.loads(jsonStr)
df=pd.DataFrame([myDict])
print("The dataframe is:")
print(df)
Output:
The json string is:
{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}
The dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
If you have a string representation of a list or dictionary and you want to convert it into a dataframe, first convert the string into a list or dictionary. For this, you can use the eval()
method. The eval()
method takes the string representation of the list or dictionary and converts them into a python list or dictionary respectively. After this, you can use the list to create a dataframe as shown below.
import pandas as pd
dictStr='{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}'
print("The dictionary string is:")
print(dictStr)
myDict=eval(dictStr)
df=pd.DataFrame([myDict])
print("The dataframe is:")
print(df)
Output:
The dictionary string is:
{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}
The dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
Conclusion
In this article, we discussed the ValueError: DataFrame constructor not properly called exception in Python. We also discussed the possible cause and solutions for this error. To learn more about python programming, you can read this article on how to overwrite a file in python. You might also like this article on CPython vs Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles!
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.