DataFrame constructor not properly called

The pd.DataFrame constructor does not accept a dictionary view as data. You can convert to list instead. Here’s a minimal example:

d = {'a': 1, 'b': 2, 'c': 3}

df = pd.DataFrame(d.values(), index=d.keys())
# PandasError: DataFrame constructor not properly called!

df = pd.DataFrame(list(d.values()), index=d.keys())
# Works!

The docs do suggest this:

data : numpy ndarray (structured or homogeneous), dict, or DataFrame

Equivalently, you can use pd.DataFrame.from_dict, which accepts a dictionary directly:

df = pd.DataFrame.from_dict(d, orient='index')

Leave a Comment

tech