ValueError: DataFrame constructor not properly called [Fix]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. ValueError: DataFrame constructor not properly called
  2. Convert view objects to a list
  3. Initializing the DataFrame with an array
  4. Constructing a DataFrame from a dictionary that is wrapped in a string

# ValueError: DataFrame constructor not properly called [Fix]

The Pandas "ValueError: DataFrame constructor not properly called" occurs when you call the pandas.DataFrame constructor with an invalid value.

To solve the error, pass a dictionary or an ndarray to the DataFrame class.

Here is an example of how the error occurs.

main.py
import pandas as pd data = 'bobbyhadz.com' # ⛔️ ValueError: DataFrame constructor not properly called! df = pd.DataFrame(data)

valueerror dataframe constructor not properly called

We passed a string instead of a dictionary or an array to the pandas.DataFrame class.

The class creates an object that stores two-dimensional, size-mutable, tabular data.

The most common way to create a DataFrame is to use a dictionary.

main.py
import pandas as pd data = { 'Name': ['Alice', 'Bobby', 'Carl'], 'Age': [29, 30, 31] } df = pd.DataFrame(data) # Name Age # 0 Alice 29 # 1 Bobby 30 # 2 Carl 31 print(df)

create dataframe using dictionary

The code for this article is available on GitHub

Notice that the keys in the dictionary are the names of the columns.

The values in the lists are the values of the corresponding column.

The dictionary can also contain Series objects.

main.py
import pandas as pd data = { 'Name': ['Alice', 'Bobby', 'Carl'], 'Age': [29, 30, 31], 'ID': pd.Series([2, 3, 4]) } df = pd.DataFrame(data) print(df)

dictionary can contain series objects

You can also use a Series object to construct a DataFrame.

main.py
import pandas as pd ser = pd.Series([1, 2, 3], index=["a", "b", "c"]) df = pd.DataFrame(data=ser, index=["a", "c"]) # 0 # a 1 # c 3 print(df)
The code for this article is available on GitHub

# Convert view objects to a list

If you got the error when using a dictionary method, such as items(), use the list() class to convert the object to a list.

main.py
import pandas as pd data = { 'Name': 'bobby hadz', 'Age': 30 } df = pd.DataFrame(list(data.items()), columns=['name', 'age']) # name age # 0 Name bobby hadz # 1 Age 30 print(df)

# Initializing the DataFrame with an array

You can also initialize the DataFrame object with a NumPy array.

main.py
import pandas as pd import numpy as np data = np.array( [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) df = pd.DataFrame(data, columns=['a', 'b', 'c']) # a b c # 0 1 2 3 # 1 4 5 6 # 2 7 8 9 print(df)

initialize dataframe with numpy array

The code for this article is available on GitHub

We used the numpy.array() method to create a 2-dimensional NumPy array.

main.py
import pandas as pd import numpy as np data = np.array( [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) # [[1 2 3] # [4 5 6] # [7 8 9]] print(data)

The second argument we passed to the DataFrame class is a list that specifies the names of the columns.

main.py
import pandas as pd import numpy as np data = np.array( [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) df = pd.DataFrame(data, columns=['a', 'b', 'c']) # a b c # 0 1 2 3 # 1 4 5 6 # 2 7 8 9 print(df)

You can also construct a DataFrame from a NumPy array with labeled columns.

main.py
import pandas as pd import numpy as np data = np.array( [(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=[("a", "i4"), ("b", "i4"), ("c", "i4")] ) # [(1, 2, 3) (4, 5, 6) (7, 8, 9)] print(data) df = pd.DataFrame(data, columns=['c', 'a']) # c a # 0 3 1 # 1 6 4 # 2 9 7 print(df)

constructing dataframe from numpy array with labeled columns

The code for this article is available on GitHub

# Constructing a DataFrame from a dictionary that is wrapped in a string

If you have a string representation of a dictionary and need to construct a DataFrame:

  1. Use the literal_eval() method to convert the string representation of a dictionary to an actual dictionary.
  2. Pass the dictionary to the DataFrame class.
main.py
from ast import literal_eval import pandas as pd dict_string = '{"Name": ["Alice", "Bobby", "Carl"], "Age": [29, 30, 31]}' a_dict = literal_eval(dict_string) # {'Name': ['Alice', 'Bobby', 'Carl'], 'Age': [29, 30, 31]} print(a_dict) print(type(a_dict)) # <class 'dict'> df = pd.DataFrame(a_dict) # Name Age # 0 Alice 29 # 1 Bobby 30 # 2 Carl 31 print(df)
The code for this article is available on GitHub

The ast.literal_eval method allows us to safely evaluate a string that contains a Python literal.

The string may consist of strings, bytes, numbers, tuples, lists, dicts, sets, booleans and None.

Once we've converted the string to a dictionary, we pass the dictionary to the DataFrame() class.

If you want to read more on converting the string representation of an object to the actual object, check out the following article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.