0

I have a dataframe that will always start out being 5 across, but it could be any height. Ie:

 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
    ...
41 42 43 44 45

I want to rearrange the dataframe to look like this:

1 2 3 4 5 6 7 8 9 10 ... 45

What's the best way of doing this? I assume there is a good one-liner that I am missing.

  • 2
    Does this answer your question? [python pandas flatten a dataframe to a list](https://stackoverflow.com/questions/25440008/python-pandas-flatten-a-dataframe-to-a-list) – woblob Sep 25 '20 at 17:24
  • `df.values.ravel()`? – Quang Hoang Sep 25 '20 at 17:25
  • It seems like it flattens to one column, rather than (in the example) 45 columns in one row. Do you have any suggestions to do this best? Transpose seems to not do it. – Conn Dickson Sep 25 '20 at 19:41
  • Although I do not understand fully why, df = np.empty([1,45]) does change the shape of df correctly.. – Conn Dickson Sep 25 '20 at 20:02

1 Answers1

0

You can try this-

df.stack().values
Fahmi
  • 37,315
  • 5
  • 22
  • 31