How to group pandas dataframe rows by column value?

How do I group the rows of a pandas dataframe by column value?

Suppose we have a data frame called df:

A   B  C
1   1a 1b
1   1c 1d
1   1e 1f
2   2a 2b
2   2c 2d
3   3a 3b
3   3c 3d

      

I would like to use groupby to create the following:

1: {[1a, 1b],
    [1c, 1d],
    [1e, 1f]}

2: {[2a,2b],
    [2c, 2d]}


3: {[3a,3b],
    [3c. 3d]}

      

I understand .loc is an option. But this is super slow for an ultra-large dataset that I am working on. This is why I thought turning it into a dictionary of lists might be better.

Thank.

+3


source to share


1 answer


You seem to need:



df = df.groupby('A')['B','C'].apply(lambda x: x.values.tolist()).to_dict()
print (df)
{1: [['1a', '1b'], ['1c', '1d'], ['1e', '1f']], 
 2: [['2a', '2b'], ['2c', '2d']], 
 3: [['3a', '3b'], ['3c', '3d']]}

      

+3


source







All Articles