> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# ValueError: If using all scalar values, you must pass an index - Pandas
- URL: https://datascientyst.com/valueerror-if-using-all-scalar-values-you-must-pass-an-index-pandas/
- Published: 2022-10-12T05:34:25.000Z
- Updated: 2023-01-15T08:43:10.000Z
- Author: John D K
- Tags: DataFrame, Pandas Error

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

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

You can find explanation and solution on the image below:

![fix-valueerror-if-using-all-scalar-values-you-must-pass-an-index](https://datascientyst.com/content/images/2023/01/fix-valueerror-if-using-all-scalar-values-you-must-pass-an-index.webp)

Quick fixes:

**(1) add index**

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

```

**(2) use vector values**

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

```

**(3) Wrap with list**

```python
dct = {'col1': ['val1'], 'col2': [2]}
pd.DataFrame(dct, index=[0])

```

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.

## Example

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

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

```python
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 scalar values. What is a scalar value?

**Definition**  
**Scalar value** is a single value. 

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

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

```

So using single values rather than lists or arrays will raise the error: `ValueError: If using all scalar values, you must pass an index`

## Solution - use vector values

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

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

```

to

```json
{
 "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()}`:

```python
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:

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

df = pd.DataFrame(dct)

```

The code above will create DataFrame without error.

|   | col1 | col2 |
| - | ---- | ---- |
| 0 | abc  | 123  |

or:

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

df = pd.DataFrame(dct)

```

which has the same output in this case:

|   | col1 | col2 |
| - | ---- | ---- |
| 0 | abc  | 123  |

## Solution - add index

If we don't pass an index, it will raise an error that the 'DataFrame()' constructor requires an index to be passed.

So the final solution is to add a index in order to solve:

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

```

```python
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.

Article starts with nice visualization and quick fixes for the error.