1

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]]
]
  • 1
    Does this answer your question? [Convert pandas dataframe to NumPy array](https://stackoverflow.com/questions/13187778/convert-pandas-dataframe-to-numpy-array) – Ananda Jan 22 '21 at 08:00
  • @Ananda - I think not, because not added `[0,0,0]` – jezrael Jan 22 '21 at 08:04
  • Adding to jezrael's point, unfortunately not, but I found an answer though which answers the 1st part of my question. But not the part turning of adding the [0,0,0] until it is a matrix with a even shape. Here is the answer that answers the 1st part: https://stackoverflow.com/questions/65840573/merge-rows-together-who-have-the-same-value-in-a-column – bigcoder431 Jan 22 '21 at 08:07

1 Answers1

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]]]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252