Is it possible to put a vector as an element of dataframe of Pandas?
Let's say I have two np vectors:
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([6, 15, 24])
Then, I wonder if I can make some 2x3 table like below.
[1,2,3] | [4,5,6] | [7,8,9]
6 | 15 | 24
you need to use the tolist
method on np.array
to get pandas to put it how you'd like.
pd.DataFrame(dict(a=a.tolist(), b=b)).T
You could pass the a
and b
as a list of lists, without needing to transpose.
In [2188]: pd.DataFrame([a.tolist(), b.tolist()])
Out[2188]:
0 1 2
0 [1, 2, 3] [4, 5, 6] [7, 8, 9]
1 6 15 24
and, more generically,
In [2202]: pd.DataFrame(x.tolist() for x in [a, b])
Out[2202]:
0 1 2
0 [1, 2, 3] [4, 5, 6] [7, 8, 9]
1 6 15 24
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With