How to Fix: if using all scalar values, you must pass an index
Last Updated :
19 Dec, 2021
In this article, we discuss how to fix the error: if using all scalar values, you must pass an index in Python.
Cases of this error occurrence:
Python3
import pandas as pd;
dict = { 'column-1' : 1 , 'column-2' : 2 ,
'column-3' : 3 , 'column-4' : 4 }
df = pd.DataFrame( dict )
print (df)
|
Output:
ValueError: If using all scalar values, you must pass an index
Reason for the error :
The column values that are going to be present must be a vector i.e lists [a,b,c], and not the scalars it should be a list as per the syntax of the function to convert it into the data frame. After converting that into the data frame the keys appear as the column headers and values i.e value of the dictionary appears as the columns.
Method 1: Fixing the error by converting the scalars as vectors
In this method, an error can be fixed where the user needs to convert the scalars as vectors by making them as lists in python.
Example:
Python3
import pandas as pd
dict = { 'column-1' : [ 1 ], 'column-2' : [ 2 ],
'column-3' : [ 3 ], 'column-4' : [ 4 ]}
df = pd.DataFrame( dict )
print (df)
|
Output:
column-1 column-2 column-3 column-4
0 1 2 3 4
Method 2: Fixing the error by specifying the index while converting it into a dataframe
In this method, an error can be fixed where the user needs to specify the index while converting it into a data frame in python language.
Syntax:
pd.DataFrame(dict,index=[0])
Python3
import pandas as pd
dict = { 'column-1' : 1 , 'column-2' : 2 ,
'column-3' : 3 , 'column-4' : 4 }
df = pd.DataFrame( dict , index = [ 0 ])
print (df)
|
Output:
column-1 column-2 column-3 column-4
0 1 2 3 4
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...