使用jupyter笔记本电脑。我从网上搜集了一些数据,我将其命名为"graphValues"。graphValues的类型是一个列表,列表中的值是字符串。我想把graphValues的数据作为一列放在一个名为"data"的新数据帧中。当我这样做时,数据帧只包含一个元素,即显示为行而非列的graphValues列表:
data=pd.DataFrame([graphValues])
print(data)
输出:
0 [10,0,0,2,0,3,2,4,4,14,11,20,12,18,43,50,20,80...
我还试着把graphValues放在字典里,如下所示:代码:
graphValues2={'Graph Val': graphValues}
data=pd.DataFrame(graphValues2)
print(data)
这给出了一个错误:
ValueError: If using all scalar values, you must pass an index<br/>
but if I add an index of lenght x, the df will just contain the same list x times (x being the lenght of graphValues of course).
如何获得以下输出?有没有办法不使用for循环?最有效的方法是什么?想要输出:
0 10
1 0
2 0
3 2
4 0
: :
: :
不要使用打印。调用没有任何函数的变量,如下所示:
data=pd.DataFrame(graphValues.split(','))
data
或者,如果上面的代码不起作用,请使用以下代码:
data = pd.DataFrame(graphValues)
data
>>> graphValues="10,0,0,2,0,3".split(",")
>>> data=pd.DataFrame(graphValues)
>>> data
0
0 10
1 0
2 0
3 2
4 0
5 3
pd.DataFrame(['r0c0','r0c1','r0c2'])
设置单个列。添加一个外部列表,Panda会认为您正在添加行(pd.DataFrame([['r0c0','r0c1','r0c2'], ['r1c0','r1c1','r1c2']])
(。由于graphValues
已经是一个列表,所以您正在执行第二个列表。