Ivan
Ivan

Reputation: 7746

DataFrame to list of lists

I have a DataFrame df with several columns, including expiry and iv. I would like to create a lol = list-of-list [[]] so that each row in the lol corresponds to a given expiry. For example, say df look like this:

df =


expiry             iv
===========================
2019-01-18,        .18
2019-01-18,        .17
2019-01-18,        .16
2019-02-18,        .14
2019-02-18,        .13
2019-02-18,        .12

the lol would look like this:

lol = [
[0.18, 0.17, 0.16],
[0.14, 0.13, 0.12]]

So each row of the lol would correspond to the corresponding date in the df.

Upvotes: 0

Views: 24

Answers (1)

BENY
BENY

Reputation: 323226

IIUC

LoL=df.groupby('expiry').iv.agg(list).tolist()
LoL
[[0.18, 0.17, 0.16], [0.14, 0.13, 0.12]]

Upvotes: 2

Related Questions