简体   繁体   中英

Convert each row in pandas dataframe to a dataframe with one column containing in each row an array of values previously in seperate columns

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM