Home > Software engineering >  How to convert a DataFrame to a List?
How to convert a DataFrame to a List?

Time:05-02

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)

CodePudding user response:

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', []]]

CodePudding user response:

Another version:

x = [[i, g[g.notna().all(1)].values[:, 1:].tolist()] for i, g in df.groupby("name")]
print(x)

Prints:

[
    ["val_1", [[2001, 3.0], [2002, 3.0]]],
    ["val_2", [[2001, 0.0], [2002, 3.0]]],
    ["val_3", [[2001, 3.0], [2002, 0.0]]],
    ["val_4", []],
]
  • Related