How to create data frame from a list of results?
我有一个这样的结果列表[1.0,3.0,5.0,2.0,0.3,1.0,1.6,0.6,0.2,0.5,1.0,0.4,0.5,1.5,2.5,1.0]。
但我需要这些数据,如下数据框架所示(对于非常长的表,我需要这样做)。
1 2 3 4 5 | a b c d 0 1.0 0.3 0.2 0.5 1 3.0 1.0 0.5 1.5 2 5.0 1.6 1.0 2.5 3 2.0 0.6 0.5 1.0 |
这就是我获取数据的方式。你能帮我吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | data1 = {"a":[1.,3.,5.,2.], "b":[4.,8.,3.,7.], "c":[5.,45.,67.,34]} df = pd.DataFrame(data1) data2 =[] for index, row in df.iterrows(): constante = row[0] for index, celda in df.iterrows(): hey = celda[0]/constante data2.append(hey) print data2 |
可以将其转换为numpy数组并重新整形:
1 2 3 4 5 6 7 8 9 | lst = [1.0, 3.0, 5.0, 2.0, 0.3, 1.0, 1.6, 0.6, 0.2, 0.5, 1.0, 0.4, 0.5, 1.5, 2.5, 1.0] pd.DataFrame(np.array(lst).reshape(4, 4).T, columns = list("abcd")) Out[94]: a b c d 0 1.0 0.3 0.2 0.5 1 3.0 1.0 0.5 1.5 2 5.0 1.6 1.0 2.5 3 2.0 0.6 0.4 1.0 |