Say I have a DataFrame that looks like this:
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=['Col 1', 'Col 2', 'Col 3']) >>> df Col 1 Col 2 Col 3 0 1 2 3 1 4 5 6 2 7 8 9 Is there a Pandas way of returning the DataFrame as a list of lists with the headers included?
I can return the headers and values as lists as follows
>>> df.columns.values.tolist() ['Col 1', 'Col 2', 'Col 3'] >>> df.values.tolist() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> df.tolist() But how could I return the following result?
[['Col 1', 'Col 2', 'Col 3'], [1, 2, 3], [4, 5, 6], [7, 8, 9]]