Building DataFrames pandas from values ​​in variables gives "ValueError: If you use all scalar values, you must pass an index"

This may be a simple question, but I cannot figure out how to do this. Suppose I have two variables as follows.

a = 2 b = 3 

I want to build a DataFrame from this:

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

This creates an error:

ValueError: if you use all scalar values, you must pass an index

I also tried:

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

This message gives the same error message.

+192
python pandas dataframe scalar
Jul 24 '13 at 16:40
source share
14 answers

The error message says that if you pass scalar values, you need to pass the index. Thus, you cannot use scalar values ​​for columns - for example. use list:

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

or use scalar values ​​and pass in the index:

 >>> df = pd.DataFrame({'A': a, 'B': b}, index=[0]) >>> df AB 0 2 3 
+306
Jul 24 '13 at 16:49
source share
β€” -

You can also use pd.DataFrame.from_records , which is more convenient if you already have a dictionary:

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

You can also set the index, if you want, by:

 df = pd.DataFrame.from_records([{ 'A':a,'B':b }], index='A') 
+34
Mar 13 '16 at 13:26
source share

You must first create a series of pandas. The second step is to convert a series of pandas into a pandas data frame.

 import pandas as pd data = {'a': 1, 'b': 2} pd.Series(data).to_frame() 

You can even specify a column name.

 pd.Series(data).to_frame('ColumnName') 
+25
Sep 12 '17 at 10:58 on
source share

You need to provide iterators as the values ​​of the Pandas DataFrame columns:

 df2 = pd.DataFrame({'A':[a],'B':[b]}) 
+7
Jul 24 '13 at 16:49
source share

Perhaps Series will provide all the features you need:

 pd.Series({'A':a,'B':b}) 

A DataFrame can be thought of as a Series collection, so you can:

  • Combining multiple rows into a single data frame (as described here )

  • Add a series variable to an existing data frame ( example here )

+5
Apr 16 '16 at 22:43
source share

I had the same problem with numpy arrays and the solution is to smooth them out:

 data = { 'b': array1.flatten(), 'a': array2.flatten(), } df = pd.DataFrame(data) 
+3
Jul 04 '18 at 11:16
source share

This is because a DataFrame has two intuitive sizes - columns and rows.

You specify only columns using dictionary keys.

If you want to specify only one-dimensional data, use the series!

+2
Jul 22 '17 at 13:53 on
source share

If you intend to convert the scalar dictionary, you need to include the index:

 import pandas as pd alphabets = {'A': 'a', 'B': 'b'} index = [0] alphabets_df = pd.DataFrame(alphabets, index=index) print(alphabets_df) 

Although an index is not required for a list dictionary, the same idea can be expanded into a list dictionary:

 planets = {'planet': ['earth', 'mars', 'jupiter'], 'length_of_day': ['1', '1.03', '0.414']} index = [0, 1, 2] planets_df = pd.DataFrame(planets, index=index) print(planets_df) 

Of course, for a list dictionary, you can build a dataframe without an index:

 planets_df = pd.DataFrame(planets) print(planets_df) 
+2
Nov 30 '17 at 19:34
source share

This is a comment on @fAx's answer: the input does not have to be a list of entries - it can also be one dictionary:

 pd.DataFrame.from_records({'a':1,'b':2}, index=[0]) ab 0 1 2 

Which seems to be equivalent:

 pd.DataFrame({'a':1,'b':2}, index=[0]) ab 0 1 2 
+1
Apr 24 '18 at 15:12
source share

The magic of pandas at work. All logic is missing.

The error message "ValueError: If using all scalar values, you must pass an index" Says that you must pass the index.

This does not necessarily mean that passing the index makes the pandas do what you want it to do.

When you pass an index, pandas will treat the dictionary keys as column names, and the values ​​as what the column should contain for each of the values ​​in the index.

 a = 2 b = 3 df2 = pd.DataFrame({'A':a,'B':b}, index=[1]) AB 1 2 3 

Passing a larger index:

 df2 = pd.DataFrame({'A':a,'B':b}, index=[1, 2, 3, 4]) AB 1 2 3 2 2 3 3 2 3 4 2 3 

An index is usually automatically generated by the data frame if it is not specified. However, pandas do not know how many lines 2 and 3 you want. However, you can be more frank about it.

 df2 = pd.DataFrame({'A':[a]*4,'B':[b]*4}) df2 AB 0 2 3 1 2 3 2 2 3 3 2 3 

The default index is 0, though.

I would recommend always passing the list dictionary to the dataframe constructor when creating data frames. This is easier to read for other developers. Pandas has a lot of caveats; do not force other developers to contact the experts in all of them to read your code.

+1
Oct 14 '18 at 6:26
source share

If you have a dictionary, you can turn it into a pandas data frame with the following line of code:

 pd.DataFrame({"key": d.keys(), "value": d.values()}) 
0
Apr 08 '16 at 2:53 on
source share

You can try:

 df2 = pd.DataFrame.from_dict({'a':a,'b':b}, orient = 'index') 

From the documentation for the argument 'orient': if the keys of the passed dict must be columns of the resulting DataFrame, pass the 'columns (by default). Otherwise, if the keys must be strings, pass' index.

0
Mar 30 '18 at 2:02
source share

You can try to wrap your dictionary in a list

my_dict = {'A':1,'B':2}

pd.DataFrame([my_dict])

  AB 0 1 2 
0
Jan 31 '19 at 11:00
source share

Just pass the word in the list:

 a = 2 b = 3 df2 = pd.DataFrame([{'A':a,'B':b}]) 
-2
Sep 26 '18 at 12:36
source share



All Articles