Reputation: 561
I have a dataset. The type of the dataset is numpy.ndarray and shape is (50,300). Then I have a df and its shape is (50,8). I would like to append dataset as one column "C" to the df. I tried to use dataset.tolist() but then it gave me many columns of dataset. So how can I do it. Here is an example of dataset and df. Those are not real data, I just copied from internet. Thanks for your advice!
Dataset:
array([[1, 1500, 1, 2],
[1, 1700, 3, 3],
[1, 2000, 2, 2],
[1, 2400, 2, 3],
[1, 2700, 3, 3]])
Df:
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
Expected output:
joined_df:
sepal_length sepal_width petal_length petal_width species C
0 5.1 3.5 1.4 0.2 setosa [1, 1500, 1, 2]
1 4.9 3.0 1.4 0.2 setosa [1, 1700, 3, 3]
2 4.7 3.2 1.3 0.2 setosa [1, 2400, 2, 3]
3 4.6 3.1 1.5 0.2 setosa [1, 2700, 3, 3]
4 5.0 3.6 1.4 0.2 setosa [1, 3000, 3, 4]
If I do dataset.tolist(),
it showed like this:
0 1 2 3
0 1 1500 1 2
1 1 1700 3 3
2 1 2000 2 2
3
4
Upvotes: 1
Views: 57
Reputation: 862731
Convert array to nested list and append to column:
df['C'] = dataset.tolist()
Upvotes: 2