create a 3d list (list of list of lists) from a pandas dataframe

421 Views Asked by At

I am trying to make a 3d list from a pandas dataframe. It has 9 columns and 1440 rows. How do I make a list consisting of all values in a row of one value from a specific row and column that is apart of a list , in a list of each row (1440) making a 3d list of shape (1440, 9, 1) (1440, 1, 9) in the most efficient way. Here's an example of the data:

+-------------------------------------------------------------------------------------------------------+
|     0          1          2          3         4         5         6        7           8         9   |
+-------------------------------------------------------------------------------------------------------+
| 2429896.0  4508569.0  3908478.0  5720432.0  470717.0  894550.0  703861.0  51111.0    0.017544   59.0  |
| 2555574.0  4740368.0  4109880.0  6021743.0  495871.0  940702.0  740383.0  1106141.0  0.058824   59.0  |
| 2555514.0  4741014.0  4110097.0  6021093.0  494950.0  939817.0  739364.0  1104789.0  0.017544   59.0  |
+-------------------------------------------------------------------------------------------------------+
3

There are 3 best solutions below

0
On BEST ANSWER

You can use a couple of list comprehensions:

import pandas as pd
df = pd.DataFrame([
        [2429896.0,  4508569.0,  3908478.0,  5720432.0,  470717.0,  894550.0,  703861.0,  51111.0,    0.017544,   59.0],
        [2555574.0,  4740368.0,  4109880.0,  6021743.0,  495871.0,  940702.0,  740383.0,  1106141.0,  0.058824,   59.0],
        [2555514.0,  4741014.0,  4110097.0,  6021093.0,  494950.0,  939817.0,  739364.0,  1104789.0,  0.017544,   59.0]
    ])
print([[[cell] for cell in row] for _, row in df.iterrows()])

Output:

[[[2429896.0], [4508569.0], [3908478.0], [5720432.0], [470717.0], [894550.0], [703861.0], [51111.0], [0.017544000000000001], [59.0]], [[2555574.0], [4740368.0], [4109880.0], [6021743.0], [495871.0], [940702.0], [740383.0], [1106141.0], [0.058824000000000001], [59.0]], [[2555514.0], [4741014.0], [4110097.0], [6021093.0], [494950.0], [939817.0], [739364.0], [1104789.0], [0.017544000000000001], [59.0]]]
0
On

There might be better ways to do it. But one way can be like this:

l1=[]
l3=[]
l2=[]

for a,b in df.iterrows():
    l1.append(b.tolist())

for a in l1:
    for b in a:
        l2.append([b])
    l3.append(l2)
    l2 = []

print l3

Output:

[[[2429896.0], [4508569.0], [3908478.0], [5720432.0], [470717.0], [894550.0], [703861.0], [51111.0], [0.017544], [59.0]], [[2555574.0], [4740368.0], [4109880.0], [6021743.0], [495871.0], [940702.0], [740383.0], [1106141.0], [0.058824], [59.0]], [[2555514.0], [4741014.0], [4110097.0], [6021093.0], [494950.0], [939817.0], [739364.0], [1104789.0], [0.017544], [59.0]]]
0
On

This is a shortcut way to do it: np.expand_dims(df.to_numpy(), axis=-1).tolist()