Adrian
Adrian

Reputation: 108

Create a lists in a list variable in pandas DataFrame after groupby

I need to create a variable based on transactional data that will be a list consisting of lists of transactions for a single client

I managed to create a variable with a list of items:

dffg = pd.DataFrame(dff.groupby(["custid", "date", "transid"]).prod_sub.apply(lambda x: [x])).reset_index()

the result:

custid  date                transid         prod_sub
1069    2001-02-03 00:00:00 1069_20010203   [[100101, 110117, 110108, 100314]]
1069    2001-02-10 00:00:00 1069_20010210   [[110217]]
1250    2001-02-04 00:00:00 1250_20010204   [[540110, 760687, 130317]]
1250    2001-02-10 00:00:00 1250_20010210   [[100109, 100205, 110411, 100102]]
4961    2001-02-05 00:00:00 4961_20010205   [[110504, 530101, 100422, 530108, 520437]]

What I need is the following:

custid  prod_sub
1069    [[100101, 110117, 110108, 100314], [110217]]
1250    [[540110, 760687, 130317], [100109, 100205, 110411, 100102]]
4961    [[110504, 530101, 100422, 530108, 520437]]

Please help

Upvotes: 1

Views: 390

Answers (1)

ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19395

something like

df.groupby('custid').prod_sub.apply(lambda x: x.tolist())

should work

Upvotes: 4

Related Questions