Petr Petrov Petr Petrov - 9 days ago 5
Python Question

Pandas: convert column to list

I have a dataframe

date member_id val
2016-06-01 2377264 14
2016-06-01 289719 6
2016-06-02 289719 12
2016-06-02 2377264 1
2016-06-03 289719 0
2016-06-04 289719 0
2016-06-05 289719 3


I need to get
member_id val
2377264 [14, 1]
289719 [6, 12, 0, 3]
And next I want to sum elements in list and if there is 0 in list, write it. I mean

member_id val
2377264 [15]
289719 [18, 0, 0, 3]


I tried

vals = []
print df.groupby('member_id')['val'].apply(lambda x: vals.append(x))


but it returns all None values in a column.
How can I fix that?

Answer

try this

1. if you want val list

df.groupby('member_id')['val'].apply(lambda x: list(x))

output

member_id
289719     [6, 12, 0, 0, 3]
2377264             [14, 1]
Name: val, dtype: object

2. To get list of list

df.groupby('member_id')['val'].apply(lambda x: list(x)).tolist()

output

[[6, 12, 0, 0, 3], [14, 1]]

3. To get dict

df.groupby('member_id')['val'].apply(lambda x: list(x)).to_dict()

output

{2377264: [14, 1], 289719: [6, 12, 0, 0, 3]}

4. To get sum

df.groupby('member_id')['val'].apply(lambda x: sum(x))

output

member_id
289719     21
2377264    15
Name: val, dtype: int64
Comments