import pandas as pd
# Create the sample dataframe
df = pd.DataFrame({
'terms': ['network good', 'good network', 'net speed', 'good rate', 'rate good'],
'sum': [1, 3, 6, 7, 70]
})
# split the strings in `term` column by space, sort the resulting list and
# join again. This will help to order the jumbled bigrams
df['terms'] = df['terms'].apply(lambda x: ' '.join(sorted(x.split(' '))))
# Do the groupby and sum the `sum` column
df = df.groupby('terms').sum().reset_index()
Фрейм входных данных
terms sum
0 network good 1
1 good network 3
2 net speed 6
3 good rate 7
4 rate good 70
Фрейм выходных данных
terms sum
0 good network 4
1 good rate 77
2 net speed 6