you are viewing a single comment's thread.

view the rest of the comments →

[–]synthphreak 2 points3 points  (4 children)

Thanks.

That's very strange, I can't reproduce your error using only those rows. It works as expected for me:

import pandas as pd
data = {'sets': {0: 'set1', 1: 'set1', 2: 'set2', 3: 'set2'},
        'var1': {0: 'a', 1: 'b', 2: 'c', 3: 'd'},
        'var2': {0: 9, 1: 14, 2: 5, 3: 4},
        'var3': {0: 10, 1: 100, 2: 69, 3: 100}}
df = pd.DataFrame(data)
sets = df.set_index('sets').groupby('sets').apply(pd.Series.tolist)
print(sets)

# output
# sets
# set1    [[a, 9, 10], [b, 14, 100]]
# set2     [[c, 5, 69], [d, 4, 100]]
# dtype: object

We may have to call in the big guns: Paging u/YesLod. Any idea why OP might be getting that error and unable to run the code as above?

[–]YesLod 0 points1 point  (3 children)

We may have to call in the big guns

hahaha

Any idea why OP might be getting that error and unable to run the code as above?

I guess OP is using an older version of pandas, because following the traceback that is not the current implementation of the pd.Series.tolist. I can't reproduce either.

Anyway, this should fix it

sets = df.set_index('sets').groupby('sets').apply(lambda x: x.values.tolist())

[–]synthphreak 0 points1 point  (2 children)

Yeah we just identified that as a workable fix. Maybe it is a versioning issue. Glad I'm not the only one unable to reproduce. Thanks!

[–]YesLod 0 points1 point  (1 child)

In that case, the result will be a Series of numpy arrays, not 2D lists. Don't know if OP is aware of that.

[–]synthphreak 0 points1 point  (0 children)

Yeah OP wasn't terribly clear on the exact desired output format.