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.
firelynx Oct 14 '18 at 6:26 2018-10-14 06:26
source share