In this tutorial, we'll take a closer look at the Pandas error:

"ValueError: If using all scalar values, you must pass an index".

First, we'll create an example of how to produce it. Next, we'll explain the leading cause of the error. And finally, we'll see how to fix it.

(1) add index

pd.DataFrame(dct, index=[0])

(2) use vector values

dct = {k:[v] for k,v in dct.items()}

(3) Wrap with list

dct = {'col1': 'abc', 'col2': 123}
pd.DataFrame(dct, index=[0])

Example

Now, let's see an example that generates a Pandas error:

ValueError: If using all scalar values, you must pass an index

import pandas as pd

dct = {'col1': 'val1', 'col2': 2}
df = pd.DataFrame(dct)

So we get the error above:

ValueError: If using all scalar values, you must pass an index

Cause

The error is raised because we pass only scaler values. What is scaler value?

Definition
Scalar value is a single value.

So the reason is that we are using a dictionary with single values.

{
 "col1": "val1",
 "col2": 2
}

Solution - use vector values

So one solution is to use vector values instead of scalar values. In practice this means turning:

{
 "col1": "val1",
 "col2": 2
}

to

{
 "col1": ["val1"],
 "col2": [2]
}

This would resolve the error. We can use dict comprehensions in order to workaround the error like - {k:[v] for k,v in dct.items()}:

dct = {'col1': 'abc', 'col2': 123}
dct = {k:[v] for k,v in dct.items()}
pd.DataFrame(dct)

This will solve the error and create DataFrame like:

col1 col2
0 abc 123

Solution - wrap with list

Another solution is to wrap the whole dictionary with list:

dct = [{'col1': 'abc', 'col2': 123}]

df = pd.DataFrame(dct)

The code above will create DataFrame without error.

col1 col2
0 abc 123

Solution - add index

The final solution is adding a index in order to solve:

ValueError: If using all scalar values, you must pass an index
dct = {'col1': 'abc', 'col2': 123}

pd.DataFrame(dct, index=[0])

Conclusion

We've explained Pandas error "ValueError: If using all scalar values, you must pass an index".

Then, we discussed how to produce the error and the cause of the problem. Lastly, we discussed several solutions to resolve the error.