根据变量中的值构造pandas DataFrame会产生"ValueError:如果使用所有标量值,则必须传递索引". [英] Constructing pandas DataFrame from values in variables gives "ValueError: If using all scalar values, you must pass an index"
本文介绍了根据变量中的值构造pandas DataFrame会产生"ValueError:如果使用所有标量值,则必须传递索引".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个简单的问题,但是我不知道该怎么做.可以说我有以下两个变量.
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
我想以此构造一个DataFrame:
I want to construct a DataFrame from this:
df2 = pd.DataFrame({'A':a,'B':b})
这会产生一个错误:
ValueError:如果使用所有标量值,则必须传递索引
ValueError: If using all scalar values, you must pass an index
我也尝试过:
df2 = (pd.DataFrame({'a':a,'b':b})).reset_index()
这将给出相同的错误消息.
This gives the same error message.
推荐答案
错误消息指出,如果要传递标量值,则必须传递索引.因此您不能对列使用标量值-例如使用列表:
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:
>>> df = pd.DataFrame({'A': [a], 'B': [b]})
>>> df
A B
0 2 3
或使用标量值并传递索引:
or use scalar values and pass an index:
>>> df = pd.DataFrame({'A': a, 'B': b}, index=[0])
>>> df
A B
0 2 3
这篇关于根据变量中的值构造pandas DataFrame会产生"ValueError:如果使用所有标量值,则必须传递索引".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文