import pandas as pd
xs = pd.DataFrame({
'batch1': {
'timestep1': [1, 2, 3],
'timestep2': [3, 2, 1]
}
}).T
xs = pd.concat((xs.explode('timestep1').drop('timestep2', axis=1), xs.explode('timestep2').drop('timestep1', axis=1)), axis=1)
print(xs, '
')
n = xs.to_numpy().reshape(1, 2, 3)
print(n)
Output:
timestep1 timestep2
batch1 1 3
batch1 2 2
batch1 3 1
[[[1 3 2]
[2 3 1]]]
EDIT
Starting from your original data frame you can do:
xs = pd.DataFrame({
('batch1','timestep1'): {
'feature1': 1,
'feature2': 2,
'feature3': 3
},
('batch1', 'timestep2'): {
'feature1': 3,
'feature2': 2,
'feature3': 1
},
('batch2','timestep1'): {
'feature1': 4,
'feature2': 5,
'feature3': 6
},
('batch2', 'timestep2'): {
'feature1': 7,
'feature2': 8,
'feature3': 9
}
}
).T
array = xs.to_numpy().reshape(2,2,3)
print(array)
Output:
[[[1 2 3]
[3 2 1]]
[[4 5 6]
[7 8 9]]]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…