ADVERTISEMENT
ADVERTISEMENT

Constructing pandas DataFrame from values in variables gives 'ValueError: If using all scalar values, you must pass an index'

Solution for Constructing pandas DataFrame from values in variables gives "ValueError: If using all scalar values, you must pass an index".
Submitted by Pranit Sharma, on April 25, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consists of rows, columns, and the data. We can create a DataFrame with the help of a dictionary but when we use scaler values or even strings as a value inside the dictionary, pandas will raise an error.

Example:

# Importing pandas package

import pandas as pd

# Creating a Dictionary and setting integers as values

dict = {'col_1':10,'col_2':20,'col_3':30}

# Creating a DataFrame

df = pd.DataFrame(dict)

# Print

print(df)

Output:

Constructing pandas DataFrame from values in variables | Output 1

Solving this error is quite easy, as it says we must pass an index means we have to pass a list of integers and set it as a value inside a dictionary like {'col_1':[10]}.

# Importing pandas package

import pandas as pd

# Creating a Dictionary and setting integers as values

dict = {'col_1':[10],'col_2':[20],'col_3':[30]}

# Creating a DataFrame

df = pd.DataFrame(dict)

# Print

print(df)

Output:

Constructing pandas DataFrame from values in variables | Output 2

Note: The same logic goes with strings also, i.e., strings too must be passed inside a list and set as a value inside a dictionary otherwise it will raise the same error.

# Importing pandas package

import pandas as pd

# Creating a Dictionary and setting integers as values

dict = {'col_1':'Hello','col_2':'dear','col_3':'World'}

# Creating a DataFrame

df = pd.DataFrame(dict)

# Print

print(df)

Error:

Constructing pandas DataFrame from values in variables | Output 3

Passing the string inside the list while creating a dictionary would resolve this error.

# Importing pandas package

import pandas as pd

# Creating a Dictionary and setting integers as values

dict = {'col_1':['Hello'],'col_2':['dear'],'col_3':['World']}

# Creating a DataFrame

df = pd.DataFrame(dict)

# Print

print(df)

Output:

Constructing pandas DataFrame from values in variables | Output 4

Python Pandas Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.