python pandas flatten a dataframe to a list python pandas flatten a dataframe to a list numpy numpy

python pandas flatten a dataframe to a list


You can use .flatten() on the DataFrame converted to a NumPy array:

df.to_numpy().flatten()

and you can also add .tolist() if you want the result to be a Python list.

Edit

In previous versions of Pandas, the values attributed was used instead of the .to_numpy() method, as mentioned in the comments below.


Maybe use stack?

df.stack().valuesarray(['1/2/2014', 'a', '3', 'z1', '1/3/2014', 'c', '1', 'x3'], dtype=object)

(Edit: Incidentally, the DF in the Q uses the first row as labels, which is why they're not in the output here.)


You can try with numpy

import numpy as npnp.reshape(df.values, (1,df.shape[0]*df.shape[1]))