I have got a parsed .csv file (I have done this by using pd.read_file). This is the csv file:
Id A B C
1 7 6 5
1 4 3 2
2 1 0 0
Is it possible to make turn that into this (in a numpy array):
[
[[7,6,5],[4,3,2]],
[[1,0,0],[0,0,0]]
]
I have got a parsed .csv file (I have done this by using pd.read_file). This is the csv file:
Id A B C
1 7 6 5
1 4 3 2
2 1 0 0
Is it possible to make turn that into this (in a numpy array):
[
[[7,6,5],[4,3,2]],
[[1,0,0],[0,0,0]]
]
First add missing rows by GroupBy.cumcount
with DataFrame.unstack
and Series.stack
and then convert to nested lists:
L = (df.set_index(['Id', df.groupby('Id').cumcount()])
.unstack(fill_value=0)
.stack()
.groupby('Id')
.apply(lambda x: x.to_numpy().tolist())
.tolist()
)
print (L)
[[[7, 6, 5], [4, 3, 2]], [[1, 0, 0], [0, 0, 0]]]