Q:

Count values of one column per value of second column in dataframe

I have this dataframe:
dummy_dataset = {'sentences': ['a','b','c','d','e','f'], 'classes': [1,2,1,3,3,2] }
dataframe = pd.DataFrame(dummy_dataset)

sentences   classes
0   a   1
1   b   2
2   c   1
3   d   3
4   e   3
5   f   2

What I am looking for is:
output = { 1 : ['a','c'], 2 : ['b','f'], 3: ['d','e'] }

I tried with dict method :
dict_count = {}

for m in range(len(dfg)):
    if dfg['classes'].iloc[m] not in dict_count:
        dict_count[dfg['classes'].iloc[m]] = [dfg['sentences'].iloc[m]]
    else:
        dict_count[dfg['classes'].iloc[m]].append(dfg['sentences'].iloc[m])

How can I do this with pandas count and groupby method?

A:

Use groupby on classes column and aggregate as list , then to_dict:
dataframe.groupby('classes')['sentences'].agg(list).to_dict()

Output:
{1: ['a', 'c'], 2: ['b', 'f'], 3: ['d', 'e']}

