Question
Follow sort after a group_by in polars
import polars as pl
# Sample data
data = {
'Group': ['A', 'A', 'B', 'B', 'C', 'C'],
'Value': [10, 20, 15, 25, 5, 30],
'OtherColumn': [100, 200, 150, 250, 50, 300]
}
# Create DataFrame
df = pl.DataFrame(data)
# Group by 'Group' and sort within each group by 'Value'
sorted_df = df.group_by('Group').map_groups(lambda group_df: group_df.sort('Value'))
# Display the sorted DataFrame
print(sorted_df)
is there a native polars way to do a sort within the groups after a group_by
without using map_groups
?
the alternative approach I know is to sort specifying multiple columns, but I would like to first group_by
, and then do the sort.
3 52
3