Last updated: Apr 11, 2024
Reading time·4 min
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.
import pandas as pd data = 'bobbyhadz.com' # ⛔️ ValueError: DataFrame constructor not properly called! df = pd.DataFrame(data)
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.
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)
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.
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)
You can also use a Series
object to construct a DataFrame
.
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)
If you got the error when using a dictionary method, such as items()
, use the
list()
class to convert the object to a list.
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)
You can also initialize the DataFrame
object with a NumPy array.
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)
We used the numpy.array() method to create a 2-dimensional NumPy array.
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.
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.
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)
If you have a string representation of a dictionary and need to construct a DataFrame:
literal_eval()
method to convert the string representation of a
dictionary to an actual dictionary.DataFrame
class.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 ast.literal_eval method allows us to safely evaluate a string that contains a Python literal.
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.
You can learn more about the related topics by checking out the following tutorials: