'Reset index error when I run the following code

df_rate_type_abc=df_rate_type.groupby(['AB10_RANGE', 'AB10_RANGING'], as_index =False).apply(lambda x: sorted(x['rate_type'].unique())).reset_index(name='rate_types')


Solution 1:[1]

For use Series.reset_index need specify column rate_type after groupby for Series:

df_rate_type_abc=(df_rate_type.groupby(['AB10_RANGE', 'AB10_RANGING'])['rate_type']
                              .apply(lambda x: sorted(x.unique()))
                              .reset_index(name='rate_types'))

Another idea is use:

df_rate_type_abc=(df_rate_type.sort_values(['AB10_RANGE','AB10_RANGING','rate_type'])
                              .groupby(['AB10_RANGE', 'AB10_RANGING'])['rate_type']
                              .unique()
                              .reset_index(name='rate_types'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1