Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Data Science by (17.6k points)

This may be a simple question, but I can not figure out how to do this. Lets say that I have two variables as follows.

a = 2

b = 3

I want to construct a DataFrame from this:

df2 = pd.DataFrame({'A':a,'B':b})

This generates an error:

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

I tried this also:

df2 = (pd.DataFrame({'a':a,'b':b})).reset_index()

This gives the same error message.

1 Answer

0 votes
by (41.4k points)

Here the error message says that for passing scalar values, you have to pass an index.

So you can either not use scalar values for the columns:

>>> df = pd.DataFrame({'A': [a], 'B': [b]})

>>> df

   A  B

0  2 3

or use you can use scalar values and pass an index:

>>> df = pd.DataFrame({'A': a, 'B': b}, index=[0])

>>> df

   A  B

0  2 3

Browse Categories