5

I have the following pandas dataframe called table and want to store all its cell values in a single list:

table = pd.DataFrame({'col-1':[2,7,13,16,23,26,27,29], 'col-2':[541,3,0,15,329,525,6,28], 'col-3':[0,571,0,9,9,0,62,0]}, index=['row-1','row-2','row-3','row-4','row-5','row-6','row-7','row-8'])

The final output should be like this:

my_list = [2,7,13,16,23,26,27,29,541,3,0,15,329,525,6,28,0,571,0,9,9,0,62,0]

Will appreciate it if you can teach me the most efficient way to undertake this task. Cheers.

Farid
  • 89
  • 5

3 Answers3

4

For better perfomance is used numpy method numpy.ravel with transposed data:

import numpy as np

L = np.ravel(table.T).tolist()

Idea with DataFrame.unstack is also possible, but if huge df performance should be worse:

L = table.unstack().tolist()

print (L)
[2, 7, 13, 16, 23, 26, 27, 29, 541, 3, 0, 15, 329, 525, 6, 28, 0, 571, 0, 9, 9, 0, 62, 0]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

You could use :

my_list = table.T.values.flatten().tolist()

tanspose .T is used to get the same order as your example

DavidK
  • 2,495
  • 3
  • 23
  • 38
0

A possible solution would be to use melt():

my_list = table.melt()['value'].tolist()
print(my_list)

Alternatively, you could also try variations of @DavidK's Answer with to_numpy(), which is recommended by pandas and also discussed in this question:

my_list = df.T.to_numpy().ravel().tolist()
print(my_list)
my_list = df.T.to_numpy().reshape(-1).tolist()
print(my_list)

Output in either case:

[2, 7, 13, 16, 23, 26, 27, 29, 541, 3, 0, 15, 329, 525, 6, 28, 0, 571, 0, 9, 9, 0, 62, 0]
Grayrigel
  • 3,474
  • 5
  • 14
  • 32