I have a dataframe with 10 columns with each elements as list of length 40.How can i convert them into numpy array of shape (-1,400) . column (click this link to see the column)
Asked
Active
Viewed 524 times
-1
-
Please add a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and don't only provide links to images. In particular, the dtype of the dataframe is important here. – Jan Wilamowski Jun 17 '21 at 06:21
-
1Look at `df.to_numpy()`. That's the array created from the frame. A clear idea of it's shape, dtype, and nature of the elements is required before you can consolidate into a more compact array. – hpaulj Jun 17 '21 at 06:27
-
Array shape of (-1,400)? What does it mean? – sharathnatraj Jun 17 '21 at 06:32
-
This question has already been asked. Check this out: https://stackoverflow.com/questions/31789160/convert-select-columns-in-pandas-dataframe-to-numpy-array. Hopefully this answers your question. – Jun 17 '21 at 06:43
-
@desertnaut The quoted link does not completely solve this question. Need to concatenate the lists within columns. – SeaBean Jun 17 '21 at 10:14
-
1@SeaBean OK, I reopened it – desertnaut Jun 17 '21 at 10:18
1 Answers
1
Try: df.apply()
+ numpy.concatenate()
+ to_numpy()
:
import numpy as np
df.apply(lambda x: np.concatenate(x), axis=1).to_numpy()
Test Run
Define a dataframe with 3 rows 10 columns each element of a list of length 40:
data = [[list(range(40))] * 10] * 3
cols = list(range(10))
df = pd.DataFrame(data, columns=cols)
Result:
Shape of whole extraction:
df.apply(lambda x: np.concatenate(x), axis=1).to_numpy().shape
(3,)
Shape of each row:
df.apply(lambda x: np.concatenate(x), axis=1).to_numpy()[0].shape
(400,)

SeaBean
- 22,547
- 3
- 13
- 25