How to convert a DataFrame to a List?

I have a DataFrame with three columns. I want to convert this DataFrame to a python List.

DataFrame:

    name       year   value  
0   val_1      2001      3      
1   val_1      2002      3  
2   val_2      2001      0    
3   val_2      2002      3  
4   val_3      2001      3  
5   val_3      2002      0
6   val_4      2001      np.nan

List:

[['val_1', [[2001, 3], [2002, 3]]], ['val_2', [[2001, 0], [2002, 3]]], ['val_3', [[2001, 3], [2002, 0]]], ['val_4', []]]

An attempt:

[[k, list(g.values[:, 1:].tolist())] for k, g in df.groupby('value')]

(I struggle to deal with the np.nan)

>Solution :

Try:

>>> [[k, v.drop("name",axis=1).dropna().astype('int').to_numpy().tolist()] for k, v in df.groupby('name')]

[['val_1', [[2001, 3], [2002, 3]]],
 ['val_2', [[2001, 0], [2002, 3]]],
 ['val_3', [[2001, 3], [2002, 0]]],
 ['val_4', []]]

Leave a Reply