I have a DataFrame that looks like:
a b
0 [1., 2., 3.] 1.
1 [4., 5., 6.] 2.
I want a 2d numpy array that takes the list from column a and appends values from column b, like so:
[[1., 2., 3., 1.],
[4., 5., 6., 2.]]
I've tried unsuccessfully:
>>>np.c_[df.a, df.b]
array([[array([1., 2., 3.], dtype=float32), 1.],
[[array([4., 5., 6.], dtype=float32), 2.]], dtype=object)
Variants of np.hstack or np.append lead to the same result.
CodePudding user response:
>>>df = pd.DataFrame(
{
'a':[np.array([1., 2., 3.]),np.array([4., 5., 6.])],
'b':[1., 2.]
})
>>>df
a b
0 [1.0, 2.0, 3.0] 1.0
1 [4.0, 5.0, 6.0] 2.0
>>>df.apply(lambda x: np.append(x.a, x.b), axis=1).apply(pd.Series).values
array([[1., 2., 3., 1.],
[4., 5., 6., 2.]])
CodePudding user response:
>>>df.apply(lambda x: x['a'] [x['b']], axis=1).apply(pd.Series).values
array([[1., 2., 3., 1.],
[4., 5., 6., 2.]])
CodePudding user response:
You can use numpy.append
with .apply()
, as follows:
df.apply(lambda x: np.append(x['a'], x['b']), axis=1).to_numpy()
Result:
array([array([1, 2, 3, 1]),
array([4, 5, 6, 2])], dtype=object)