I have a dataframe that looks like this:
0 a b c d
1 b c d e
2 f o n e
3 o b l e
And I want to convert it into this:
0[a,b,c,d]
1[b,c,d,e]
2[f,o,n,e]
3[o,b,l,e]
Such that I go from x columns (4 in this case) to an array containing those values in a single column.
Does anyone know how this can be done?
CodePudding user response:
To aggregate many columns into a Series of lists, use agg
on axis=1
:
s = df.agg(list, axis=1)
output:
0 [a, b, c, d]
1 [b, c, d, e]
2 [f, o, n, e]
3 [o, b, l, e]
dtype: object
CodePudding user response:
You can use df.apply(list, 1).to_frame()
. Consider if you actually want that, though. Having lists in dataframes defeats the purpose of pandas - fast vectorized operations on tabular data.
edit: or just df.to_numpy()
depending on what your plan is.