Python
Pandas
DataFrame
ValueError
IndexError

Constructing DataFrame from values in variables yields ValueError If using all scalar values, you must pass an index

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This pandas error appears when you try to build a DataFrame from a dictionary of scalar values and pandas cannot infer how many rows you want. A scalar such as 42 or "alice" is just one value, so if every column value is scalar, you must either supply an index or wrap the values in lists to make the row structure explicit.

Why the Error Happens

This code raises the error:

python
1import pandas as pd
2
3name = "Ava"
4age = 29
5
6df = pd.DataFrame({
7    "name": name,
8    "age": age,
9})

Pandas complains because both values are scalars. It knows the column names, but it does not know how many rows to create.

That is what the message means:

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

Fix 1: Wrap Each Value in a List

If your intention is to create a single-row DataFrame, the most common fix is to wrap each scalar in a list.

python
1import pandas as pd
2
3name = "Ava"
4age = 29
5
6df = pd.DataFrame({
7    "name": [name],
8    "age": [age],
9})
10
11print(df)

Now pandas can see that each column has one row of data.

Fix 2: Pass an Explicit Index

Another valid solution is to keep the scalar values but provide an index that tells pandas how many rows to create.

python
1import pandas as pd
2
3name = "Ava"
4age = 29
5
6df = pd.DataFrame({
7    "name": name,
8    "age": age,
9}, index=[0])
10
11print(df)

This also creates a one-row DataFrame. Some developers prefer this style because it leaves the scalar values visually unchanged.

Fix 3: Use a List of Dictionaries

If you are constructing rows, a list of record dictionaries is often the clearest shape.

python
1import pandas as pd
2
3row = {
4    "name": "Ava",
5    "age": 29,
6}
7
8df = pd.DataFrame([row])
9print(df)

This scales naturally when you later add more rows:

python
1rows = [
2    {"name": "Ava", "age": 29},
3    {"name": "Liam", "age": 34},
4]
5
6df = pd.DataFrame(rows)
7print(df)

That is often the most readable option when the data really represents records rather than columns assembled separately.

Know What Shape You Intend

A lot of confusion disappears once you decide whether you are building:

  • one row
  • one column
  • several rows
  • several columns

For example, if you intended a single column instead of a single row, a Series might be the better object:

python
1import pandas as pd
2
3s = pd.Series({"name": "Ava", "age": 29})
4print(s)

Not every collection of values should become a DataFrame immediately.

Common Pitfalls

One common mistake is assuming pandas will automatically treat scalar values as one row. It cannot safely guess that intention when all values are scalars.

Another is mixing scalars and lists accidentally. That can create confusing shape errors or mismatched column lengths that are harder to interpret than the original message.

Developers also sometimes choose DataFrame when a Series or plain dictionary would be simpler. If the data is not tabular yet, forcing it into a table too early can make the code harder to follow.

Finally, remember that index=[0] and list-wrapped values solve the same basic problem. Choose the form that best matches the data shape you want to communicate.

Summary

  • The error happens because pandas cannot infer row count from all-scalar column values.
  • Wrap scalar values in lists when you want a one-row DataFrame.
  • Or pass an explicit index such as index=[0].
  • A list of dictionaries is often the cleanest shape for record-style data.
  • If the data is not truly tabular yet, consider using a Series or plain dictionary instead.

Course illustration
Course illustration

All Rights Reserved.