我正在尝试从pandas数据框中创建一个3d列表。它有9列和1440行。如何创建一个列表,该列表包含列表中每个行(1440)的特定行和列中一个值的一行中的所有值(1340),形成一个3d列表的形状(1440, 9,1)(1440,1,9)以最有效的方式。以下是数据的示例:
+-------------------------------------------------------------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 |
+-------------------------------------------------------------------------------------------------------+
| 2429896.0 4508569.0 3908478.0 5720432.0 470717.0 894550.0 703861.0 51111.0 0.017544 59.0 |
| 2555574.0 4740368.0 4109880.0 6021743.0 495871.0 940702.0 740383.0 1106141.0 0.058824 59.0 |
| 2555514.0 4741014.0 4110097.0 6021093.0 494950.0 939817.0 739364.0 1104789.0 0.017544 59.0 |
+-------------------------------------------------------------------------------------------------------+
答案 0 :(得分:1)
您可以使用几个列表推导:
import pandas as pd
df = pd.DataFrame([
[2429896.0, 4508569.0, 3908478.0, 5720432.0, 470717.0, 894550.0, 703861.0, 51111.0, 0.017544, 59.0],
[2555574.0, 4740368.0, 4109880.0, 6021743.0, 495871.0, 940702.0, 740383.0, 1106141.0, 0.058824, 59.0],
[2555514.0, 4741014.0, 4110097.0, 6021093.0, 494950.0, 939817.0, 739364.0, 1104789.0, 0.017544, 59.0]
])
print([[[cell] for cell in row] for _, row in df.iterrows()])
输出:
[[[2429896.0], [4508569.0], [3908478.0], [5720432.0], [470717.0], [894550.0], [703861.0], [51111.0], [0.017544000000000001], [59.0]], [[2555574.0], [4740368.0], [4109880.0], [6021743.0], [495871.0], [940702.0], [740383.0], [1106141.0], [0.058824000000000001], [59.0]], [[2555514.0], [4741014.0], [4110097.0], [6021093.0], [494950.0], [939817.0], [739364.0], [1104789.0], [0.017544000000000001], [59.0]]]
答案 1 :(得分:0)
可能有更好的方法来做到这一点。但有一种方法可以是这样的:
l1=[]
l3=[]
l2=[]
for a,b in df.iterrows():
l1.append(b.tolist())
for a in l1:
for b in a:
l2.append([b])
l3.append(l2)
l2 = []
print l3
输出:
[[[2429896.0], [4508569.0], [3908478.0], [5720432.0], [470717.0], [894550.0], [703861.0], [51111.0], [0.017544], [59.0]], [[2555574.0], [4740368.0], [4109880.0], [6021743.0], [495871.0], [940702.0], [740383.0], [1106141.0], [0.058824], [59.0]], [[2555514.0], [4741014.0], [4110097.0], [6021093.0], [494950.0], [939817.0], [739364.0], [1104789.0], [0.017544], [59.0]]]
答案 2 :(得分:0)
这是执行此操作的快捷方式:
np.expand_dims(df.to_numpy(), axis=-1).tolist()