Python Pandas

How to Fix ValueError- If using all scalar values, you must pass an index

In pandas, a ValueError: If using all scalar values, you must pass an index is raised if a DataFrame is created from scalars without specifying the index or index labels. Scalar values are not used to create DataFrame without specifying the index. To fix this error, first approach is to specify the index while creating the DataFrame with scalars. Without specifying the index, DataFrame is created from scalars by transforming them into List. The last approach is to create a DataFrame from scalars by passing them as values to the dictionary. Then, the dictionary is passed to the DataFrame() constructor to create DataFrame.

Reproducing the Error

Create the scalars ‘marks1,’ ‘marks2,’ and ‘marks3.’ Try to create DataFrame named ‘subjects’ from these values as columns with the names – Subject1, Subject2 and Subject3.

import pandas

# Define 3 scalar values
marks1 = 78
marks2 = 90
marks3 = 98

# Try to create subjects DataFrame from the above scalar values
subjects = pandas.DataFrame({'Subject1': marks1, 'Subject2': marks2, 'Subject3': marks3})

Output

You can see that DataFrame is not created due to the ValueError: If using all scalar values, you must pass an index.

Solution 1: Passing Index

When creating a DataFrame, we can set the index labels using the pandas.DataFrame.index property. Specify the index if you are passing the scalars to the pandas DataFrame.

Syntax:

Look at the syntax below specifying the index to the DataFrame when creating the DataFrame itself.

pandas.DataFrame({'Column1’: scalar, 'Column2’: scalar} ,... ,index=[index_label])

Example

Create the ‘subjects’ DataFrame by passing the index label as ‘Row-1.’

import pandas

# Define 3 scalar values
marks1 = 78
marks2 = 90
marks3 = 98

# Create subjects DataFrame from the above scalar values by specifying the index
subjects = pandas.DataFrame({'Subject1': marks1, 'Subject2': marks2, 'Subject3': marks3},index=['Row-1'])
print(subjects)

Output

The DataFrame is created with one row with the specified index.

Solution 2: Convert scalars to List

Another solution is to transform the scalar values into a List and pass to the DataFrame directly.

[scalar]

Example

Create the ‘subjects’ DataFrame by transforming the three scalars into a List.

import pandas

# Define 3 scalar values in a List
marks1 = [78]
marks2 = [90]
marks3 = [98]

# Create DataFrame
subjects = pandas.DataFrame({'Subject1': marks1, 'Subject2': marks2, 'Subject3': marks3})
print(subjects)

Output

The DataFrame is created with one row, and the default index is 0.

Solution 3: Place scalars in Dictionary

First, scalars are passed to the dictionary as values. The keys will be DataFrame columns. After that, we will pass this dictionary to the DataFrame inside a List.

pandas.DataFrame([Dictionary])

Example:

Pass the scalar values as keys to the dictionary ‘subject_dict’ and create DataFrame with this dictionary.

import pandas

# Define 3 scalar values
marks1 = 78
marks2 = 90
marks3 = 98

# Pass the scalar values as keys to the dictionary
subject_dict = {'Subject1': marks1, 'Subject2': marks2, 'Subject3': marks3}

# Create subjects DataFrame from the above dictionary
subjects = pandas.DataFrame([subject_dict])
print(subjects)

Output:

The DataFrame is created from the dictionary.

Conclusion

The error – ValueError: If using all scalar values, you must pass an index is fixed simply by specifying the index when creating the DataFrame with scalars. Apart from this, we can create a DataFrame from scalars by transforming them into separate Lists. The last solution that we discussed to fix this error is passing the scalars as values to a dictionary and passing this dictionary to the DataFrame() constructor.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain