使用常量值初始化数据不起作用,
pd.DataFrame(0, index=[1,2,3]) # doesnt work!
# OR
pd.DataFrame(0) # doesnt work!而我观察到
(1)具有常数值的系列初始化工作
pd.Series(0, index=[1,2,3]) # Works fine!(2)初始化DataFrame不起作用
pd.DataFrame(None, index=[1,2,3]) # Works fine!(3)当没有提供索引和列时,初始化DataFrame
pd.DataFrame([1, 2, 3]) # Works fine!
pd.DataFrame([0]) # Works fine!有人知道为什么吗?
我很想从设计的角度了解更多,而不是回答“如果你检查熊猫代码,你会发现其中一项检查失败了,数据维数预计会超过1.等等”。
我认为它应该直观地发挥作用(考虑到熊猫在没有提供缺省值和索引时是聪明的,而且还可以根据所提供的数据猜测尺寸)。
这种行为可能有一些原因,但无法理解。
发布于 2017-07-26 05:55:34
pd.DataFrame是二维的.当您指定
pd.DataFrame(0, index=[1, 2, 3])您正在告诉构造函数将0分配给索引为1、2和3的每一行。但是柱子是什么呢?你没有定义任何列。
你可以做两件事
选项1
指定列
pd.DataFrame(0, index=[1, 2, 3], columns=['x', 'y'])
x y
1 0 0
2 0 0
3 0 0选项2
传递一个值列表
pd.DataFrame([[0]], index=[1, 2, 3])
0
1 0
2 0
3 0https://stackoverflow.com/questions/45317279
复制相似问题