Reputation: 3018
I have a dataframe which I obtain after doing certain operations. This is how my dataframe looks like
date metric stat val device
0 2018-03-21 cpu mean 76.724406 10.41.100.1
3 2018-03-21 cpu std 124.285789 10.41.100.1
Now I want to convert it to a list of strings stored inside a list, something like below
lis = [["2018-03-21", "cpu", "mean", "76.724406", "10.41.100.1"],
["2018-03-21", "cpu", "mean", "124.285789", "10.41.100.1"]]
I did something like this
for i in df:
print(df[i].tolist())
But I get something like this
[Timestamp('2018-03-21 00:00:00'), Timestamp('2018-03-21 00:00:00')]
['cpu', 'cpu']
['mean', 'std']
[76.72440613174048, 124.28578926665278]
['10.41.100.1', '10.41.100.1']
But I want the format to be like mentioned above. How can I do this?
Upvotes: 1
Views: 3056
Reputation: 164613
The accepted solution by jezrael works.
However, I would suggest you take advantage of the numpy
representation directly, since this is a more efficient way of storing and manipulating data.
It is rarely the case that a list is more useful than the numpy
representation when dealing with structured data.
You can do this via:
res = df.values.astype(str)
This returns dtype <U11
, while df.astype(str).values
returns dtype Object
. This is a significant difference in how the data is stored internally.
Upvotes: 1
Reputation: 862396
I think need astype
for string
s first, then convert DataFrame
to numpy array
by values
and last for list
call numpy.ndarray.tolist
:
lis = df.astype(str).values.tolist()
print (lis)
[['2018-03-21', 'cpu', 'mean', '76.724406', '10.41.100.1'],
['2018-03-21', 'cpu', 'std', '124.285789', '10.41.100.1']]
Upvotes: 1