-1

I have the dataframe

df = 
c1 c2 c3 c4 c5
1.  2. 3. 1. 5
8.  2. 1. 3. 8
4.  9. 1  2. 3

And I want to group all columns to a single list that will be the only columns, so I will get:

df = 
    l
[1,2,3,1,5]
[8,2,1,3,8]
[4,9,1,2,3]

(Shape of df was change from (3,5) to (3,1))

What is the best way to do this?

Cranjis
  • 1,590
  • 8
  • 31
  • 64

1 Answers1

3

Try:

#best way:
df['l']=df.values.tolist()
#OR
df['l']=df.to_numpy().tolist()


#another way:
df['l']=df.agg(list,1)
#OR
df['l']=df.apply(list,1)
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41