patternpythonCriticalCanonical
Constructing DataFrame from values in variables yields "ValueError: If using all scalar values, you must pass an index"
Viewed 0 times
variablesindexscalaryoufrommustvalueerrordataframeusingyields
Problem
I have two variables as follows.
I want to construct a DataFrame from this:
This generates an error:
I tried this also:
This gives the same error message. How do I do what I want?
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. How do I do what I want?
Solution
The error message says that if you're passing scalar values, you have to pass an index. So you can either not use scalar values for the columns -- e.g. use a list:
or use scalar values and pass an index:
>>> df = pd.DataFrame({'A': [a], 'B': [b]})
>>> df
A B
0 2 3or use scalar values and pass an index:
>>> df = pd.DataFrame({'A': a, 'B': b}, index=[0, 3])
>>> df
A B
0 2 3
3 2 3Code Snippets
>>> df = pd.DataFrame({'A': [a], 'B': [b]})
>>> df
A B
0 2 3>>> df = pd.DataFrame({'A': a, 'B': b}, index=[0, 3])
>>> df
A B
0 2 3
3 2 3Context
Stack Overflow Q#17839973, score: 1258
Revisions (0)
No revisions yet.